home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / calendar / calendar.el.z / calendar.el
Encoding:
Text File  |  1998-05-21  |  122.9 KB  |  3,065 lines

  1. ;;; calendar.el --- Calendar functions.  -*-byte-compile-dynamic: t;-*-
  2.  
  3. ;;; Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994 Free Software
  4. ;;; Foundation, Inc.
  5.  
  6. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  7. ;; Keywords: calendar
  8. ;; Human-Keywords: calendar, Gregorian calendar, Julian calendar, 
  9. ;;    Hebrew calendar, Islamic calendar, ISO calendar, Julian day number,
  10. ;;    diary, holidays
  11.  
  12. ;; This file is part of XEmacs.
  13.  
  14. ;; XEmacs is free software; you can redistribute it and/or modify it
  15. ;; under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; XEmacs is distributed in the hope that it will be useful, but
  20. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. ;; General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  26. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This collection of functions implements a calendar window.  It
  31. ;; generates a calendar for the current month, together with the previous
  32. ;; and coming months, or for any other three-month period.  The calendar
  33. ;; can be scrolled forward and backward in the window to show months in
  34. ;; the past or future; the cursor can move forward and backward by days,
  35. ;; weeks, or months, making it possible, for instance, to jump to the
  36. ;; date a specified number of days, weeks, or months from the date under
  37. ;; the cursor.  The user can display a list of holidays and other notable
  38. ;; days for the period shown; the notable days can be marked on the
  39. ;; calendar, if desired.  The user can also specify that dates having
  40. ;; corresponding diary entries (in a file that the user specifies) be
  41. ;; marked; the diary entries for any date can be viewed in a separate
  42. ;; window.  The diary and the notable days can be viewed independently of
  43. ;; the calendar.  Dates can be translated from the (usual) Gregorian
  44. ;; calendar to the day of the year/days remaining in year, to the ISO
  45. ;; commercial calendar, to the Julian (old style) calendar, to the Hebrew
  46. ;; calendar, to the Islamic calendar, to the French Revolutionary calendar,
  47. ;; to the Mayan calendar, and to the astronomical (Julian) day number.
  48. ;; When floating point is available, times of sunrise/sunset can be displayed,
  49. ;; as can the phases of the moon.  Appointment notification for diary entries
  50. ;; is available.
  51.  
  52. ;; The following files are part of the calendar/diary code:
  53.  
  54. ;;       cal-menu.el                   Menu support
  55. ;;       cal-x.el                      X-windows dedicated frame functions
  56. ;;       diary-lib.el, diary-ins.el    Diary functions
  57. ;;       holidays.el                   Holiday functions
  58. ;;       cal-french.el                 French Revolutionary calendar
  59. ;;       cal-mayan.el                  Mayan calendars
  60. ;;       cal-dst.el                    Daylight savings time rules
  61. ;;       solar.el                      Sunrise/sunset, equinoxes/solstices
  62. ;;       lunar.el                      Phases of the moon
  63. ;;       appt.el                       Appointment notification
  64.  
  65. ;; Comments, corrections, and improvements should be sent to
  66. ;;  Edward M. Reingold               Department of Computer Science
  67. ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  68. ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  69. ;;                                   Urbana, Illinois 61801
  70.  
  71. ;; GNU Emacs users too numerous to list pointed out a variety of problems
  72. ;; with earlier forms of the `infinite' sliding calendar and suggested some
  73. ;; of the features included in this package.  Especially significant in this
  74. ;; regard was the suggestion of mark-diary-entries and view-diary-entries,
  75. ;; together ideas for their implementation, by
  76. ;;  Michael S. Littman             Cognitive Science Research Group
  77. ;;  (201) 829-5155                   Bell Communications Research
  78. ;;  mlittman@wind.bellcore.com       445 South St. Box 1961 (2L-331)
  79. ;;                                   Morristown, NJ  07960
  80.  
  81. ;; The algorithms for the Hebrew calendar are those of the Rambam (Rabbi Moses
  82. ;; Maimonides), from his Mishneh Torah, as implemented by
  83. ;;  Nachum Dershowitz                Department of Computer Science
  84. ;;  (217) 333-4219                   University of Illinois at Urbana-Champaign
  85. ;;  nachum@cs.uiuc.edu               1304 West Springfield Avenue
  86. ;;                                   Urbana, Illinois 61801
  87.  
  88. ;; Technical details of all the calendrical calculations can be found in
  89.  
  90. ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold,
  91. ;; Software--Practice and Experience, Volume 20, Number 9 (September, 1990),
  92. ;; pages 899-928.  ``Calendrical Calculations, Part II: Three Historical
  93. ;; Calendars'' by E. M. Reingold,  N. Dershowitz, and S. M. Clamen,
  94. ;; Software--Practice and Experience, Volume 23, Number 4 (April, 1993),
  95. ;; pages 383-404.
  96.  
  97. ;; Hard copies of these two papers can be obtained by sending email to
  98. ;; reingold@cs.uiuc.edu with the SUBJECT "send-paper-cal" (no quotes) and
  99. ;; the message BODY containing your mailing address (snail).
  100.  
  101. ;;; Code:
  102.  
  103. (defgroup diary nil
  104.   "Diary customization"
  105.   :group 'calendar)
  106.  
  107. (defgroup holidays nil
  108.   "Holidays in calendar"
  109.   :group 'calendar)
  110.  
  111. (defun calendar-version ()
  112.   (interactive)
  113.   (message "Version 5.3, January 25, 1994"))
  114.  
  115. (defgroup appt nil
  116.   "Appointment notification"
  117.   :tag "Appointments"
  118.   :group 'calendar)
  119.  
  120.  
  121. ;;;###autoload
  122. (defcustom calendar-week-start-day 0
  123.   "*The day of the week on which a week in the calendar begins.
  124. 0 means Sunday (default), 1 means Monday, and so on."
  125.   :type 'integer
  126.   :group 'calendar)
  127.  
  128. ;;;###autoload
  129. (defcustom calendar-offset 0
  130.   "*The offset of the principal month from the center of the calendar window.
  131. 0 means the principal month is in the center (default), -1 means on the left,
  132. +1 means on the right.  Larger (or smaller) values push the principal month off
  133. the screen."
  134.   :type 'integer
  135.   :group 'calendar)
  136.  
  137. ;;;###autoload
  138. (defcustom view-diary-entries-initially nil
  139.   "*Non-nil means display current date's diary entries on entry.
  140. The diary is displayed in another window when the calendar is first displayed,
  141. if the current date is visible.  The number of days of diary entries displayed
  142. is governed by the variable `number-of-diary-entries'."
  143.   :type 'boolean
  144.   :group 'diary)
  145.  
  146. ;;;###autoload
  147. (defcustom number-of-diary-entries 1
  148.   "*Specifies how many days of diary entries are to be displayed initially.
  149. This variable affects the diary display when the command M-x diary is used,
  150. or if the value of the variable `view-diary-entries-initially' is t.  For
  151. example, if the default value 1 is used, then only the current day's diary
  152. entries will be displayed.  If the value 2 is used, then both the current
  153. day's and the next day's entries will be displayed.
  154.  
  155. The value can also be a vector such as [0 2 2 2 2 4 1]; this value
  156. says to display no diary entries on Sunday, the display the entries
  157. for the current date and the day after on Monday through Thursday,
  158. display Friday through Monday's entries on Friday, and display only
  159. Saturday's entries on Saturday.
  160.  
  161. This variable does not affect the diary display with the `d' command
  162. from the calendar; in that case, the prefix argument controls the
  163. number of days of diary entries displayed."
  164.   :type 'integer
  165.   :group 'diary)
  166.  
  167. ;;;###autoload
  168. (defcustom mark-diary-entries-in-calendar nil
  169.   "*Non-nil means mark dates with diary entries, in the calendar window.
  170. The marking symbol is specified by the variable `diary-entry-marker'."
  171.   :type 'boolean
  172.   :group 'diary)
  173.  
  174. (defcustom diary-entry-marker
  175.   (progn
  176.     (make-face 'diary-face)
  177.     (cond ((face-differs-from-default-p 'diary-face) nil)
  178.       (t (set-face-foreground 'diary-face "red" 'global '(x color))
  179.          (set-face-highlight-p 'diary-face t 'global 'tty)
  180.          ;; avoid a weird problem when byte-compiling appt.el
  181.          ;; in batch mode.
  182.          (if (and (not noninteractive) (fboundp 'x-make-font-bold))
  183.          (let ((bfont (x-make-font-bold
  184.                    (face-font-instance 'default)))
  185.                (mono-tag (list 'x 'mono))
  186.                (gray-tag (list 'x 'grayscale)))
  187.            (if bfont
  188.                (progn
  189.              (set-face-font 'diary-face bfont 'global mono-tag)
  190.              (set-face-font 'diary-face bfont 'global
  191.                     gray-tag)))))))
  192.     'diary-face)
  193.   "*Used to mark dates that have diary entries.
  194. Can be either a single-character string or a face."
  195.   :type '(choice string face)
  196.   :group 'diary)
  197.  
  198. (defcustom calendar-today-marker
  199.   (progn
  200.     (make-face 'calendar-today-face)
  201.     (if (not (face-differs-from-default-p 'calendar-today-face))
  202.     (set-face-underline-p 'calendar-today-face t))
  203.     'calendar-today-face)
  204.   "*Used to mark today's date.
  205. Can be either a single-character string or a face."
  206.   :type '(choice string face)
  207.   :group 'calendar)
  208.  
  209. (defcustom calendar-holiday-marker
  210.   (progn
  211.     (make-face 'holiday-face)
  212.     (cond ((face-differs-from-default-p 'holiday-face) nil)
  213.       (t (let ((color-tag (list 'x 'color))
  214.            (mono-tag (list 'x 'mono))
  215.            (gray-tag (list 'x 'grayscale)))
  216.            (set-face-background 'holiday-face [default foreground] 'global
  217.                     mono-tag)
  218.            (set-face-foreground 'holiday-face [default background] 'global
  219.                     mono-tag)
  220.            (set-face-background 'holiday-face [default foreground] 'global
  221.                     gray-tag)
  222.            (set-face-foreground 'holiday-face [default background] 'global
  223.                     gray-tag)
  224.            (set-face-background 'holiday-face "pink" 'global color-tag)
  225.            (set-face-reverse-p 'holiday-face t 'global 'tty))))
  226.     'holiday-face)
  227.   "*Used to mark notable dates in the calendar.
  228. Can be either a single-character string or a face."
  229.   :type '(choice string face)
  230.   :group 'holidays)
  231.  
  232. ;;;###autoload
  233. (defcustom view-calendar-holidays-initially nil
  234.   "*Non-nil means display holidays for current three month period on entry.
  235. The holidays are displayed in another window when the calendar is first
  236. displayed."
  237.   :type 'boolean
  238.   :group 'holidays)
  239.  
  240. ;;;###autoload
  241. (defcustom mark-holidays-in-calendar nil
  242.   "*Non-nil means mark dates of holidays in the calendar window.
  243. The marking symbol is specified by the variable `calendar-holiday-marker'."
  244.   :type 'boolean
  245.   :group 'holidays)
  246.  
  247. ;;;###autoload
  248. (defcustom all-hebrew-calendar-holidays nil
  249.   "*If nil, show only major holidays from the Hebrew calendar.
  250. This means only those Jewish holidays that appear on secular calendars.
  251.  
  252. If t, show all the holidays that would appear in a complete Hebrew calendar."
  253.   :type 'boolean
  254.   :group 'holidays)
  255.  
  256. ;;;###autoload
  257. (defcustom all-christian-calendar-holidays nil
  258.   "*If nil, show only major holidays from the Christian calendar.
  259. This means only those Christian holidays that appear on secular calendars.
  260.  
  261. If t, show all the holidays that would appear in a complete Christian
  262. calendar."
  263.   :type 'boolean
  264.   :group 'holidays)
  265.  
  266. ;;;###autoload
  267. (defcustom all-islamic-calendar-holidays nil
  268.   "*If nil, show only major holidays from the Islamic calendar.
  269. This means only those Islamic holidays that appear on secular calendars.
  270.  
  271. If t, show all the holidays that would appear in a complete Islamic
  272. calendar."
  273.   :type 'boolean
  274.   :group 'holidays)
  275.  
  276. ;;;###autoload
  277. (defcustom calendar-load-hook nil
  278.   "*List of functions to be called after the calendar is first loaded.
  279. This is the place to add key bindings to `calendar-mode-map'."
  280.   :type 'hook
  281.   :group 'calendar)
  282.  
  283. ;;;###autoload
  284. (defcustom initial-calendar-window-hook nil
  285.   "*List of functions to be called when the calendar window is first opened.
  286. The functions invoked are called after the calendar window is opened, but
  287. once opened is never called again.  Leaving the calendar with the `q' command
  288. and reentering it will cause these functions to be called again."
  289.   :type 'hook
  290.   :group 'calendar)
  291.  
  292. ;;;###autoload
  293. (defcustom today-visible-calendar-hook nil
  294.   "*List of functions called whenever the current date is visible.
  295. This can be used, for example, to replace today's date with asterisks; a
  296. function `calendar-star-date' is included for this purpose:
  297.     (setq today-visible-calendar-hook 'calendar-star-date)
  298. It can also be used to mark the current date with `calendar-today-marker';
  299. a function is also provided for this:
  300.     (setq today-visible-calendar-hook 'calendar-mark-today)
  301.  
  302. The corresponding variable `today-invisible-calendar-hook' is the list of
  303. functions called when the calendar function was called when the current
  304. date is not visible in the window.
  305.  
  306. Other than the use of the provided functions, the changing of any
  307. characters in the calendar buffer by the hooks may cause the failure of the
  308. functions that move by days and weeks."
  309.   :type 'hook
  310.   :group 'calendar)
  311.  
  312. ;;;###autoload
  313. (defcustom today-invisible-calendar-hook nil
  314.   "*List of functions called whenever the current date is not visible.
  315.  
  316. The corresponding variable `today-visible-calendar-hook' is the list of
  317. functions called when the calendar function was called when the current
  318. date is visible in the window.
  319.  
  320. Other than the use of the provided functions, the changing of any
  321. characters in the calendar buffer by the hooks may cause the failure of the
  322. functions that move by days and weeks."
  323.   :type 'hook
  324.   :group 'calendar)
  325.  
  326. ;;;###autoload
  327. (defcustom diary-file "~/diary"
  328.   "*Name of the file in which one's personal diary of dates is kept.
  329.  
  330. The file's entries are lines in any of the forms
  331.  
  332.             MONTH/DAY
  333.             MONTH/DAY/YEAR
  334.             MONTHNAME DAY
  335.             MONTHNAME DAY, YEAR
  336.             DAYNAME
  337.  
  338. at the beginning of the line; the remainder of the line is the diary entry
  339. string for that date.  MONTH and DAY are one or two digit numbers, YEAR is
  340. a number and may be written in full or abbreviated to the final two digits.
  341. If the date does not contain a year, it is generic and applies to any year.
  342. DAYNAME entries apply to any date on which is on that day of the week.
  343. MONTHNAME and DAYNAME can be spelled in full, abbreviated to three
  344. characters (with or without a period), capitalized or not.  Any of DAY,
  345. MONTH, or MONTHNAME, YEAR can be `*' which matches any day, month, or year,
  346. respectively.
  347.  
  348. The European style (in which the day precedes the month) can be used
  349. instead, if you execute `european-calendar' when in the calendar, or set
  350. `european-calendar-style' to t in your .emacs file.  The European forms are
  351.  
  352.             DAY/MONTH
  353.             DAY/MONTH/YEAR
  354.             DAY MONTHNAME
  355.             DAY MONTHNAME YEAR
  356.             DAYNAME
  357.  
  358. To revert to the default American style from the European style, execute
  359. `american-calendar' in the calendar.
  360.  
  361. A diary entry can be preceded by the character
  362. `diary-nonmarking-symbol' (ordinarily `&') to make that entry
  363. nonmarking--that is, it will not be marked on dates in the calendar
  364. window but will appear in a diary window.
  365.  
  366. Multiline diary entries are made by indenting lines after the first with
  367. either a TAB or one or more spaces.
  368.  
  369. Lines not in one the above formats are ignored.  Here are some sample diary
  370. entries (in the default American style):
  371.  
  372.      12/22/1988 Twentieth wedding anniversary!!
  373.      &1/1. Happy New Year!
  374.      10/22 Ruth's birthday.
  375.      21: Payday
  376.      Tuesday--weekly meeting with grad students at 10am
  377.               Supowit, Shen, Bitner, and Kapoor to attend.
  378.      1/13/89 Friday the thirteenth!!
  379.      &thu 4pm squash game with Lloyd.
  380.      mar 16 Dad's birthday
  381.      April 15, 1989 Income tax due.
  382.      &* 15 time cards due.
  383.  
  384. If the first line of a diary entry consists only of the date or day name with
  385. no trailing blanks or punctuation, then that line is not displayed in the
  386. diary window; only the continuation lines is shown.  For example, the
  387. single diary entry
  388.  
  389.      02/11/1989
  390.       Bill Blattner visits Princeton today
  391.       2pm Cognitive Studies Committee meeting
  392.       2:30-5:30 Lizzie at Lawrenceville for `Group Initiative'
  393.       4:00pm Jamie Tappenden
  394.       7:30pm Dinner at George and Ed's for Alan Ryan
  395.       7:30-10:00pm dance at Stewart Country Day School
  396.  
  397. will appear in the diary window without the date line at the beginning.  This
  398. facility allows the diary window to look neater, but can cause confusion if
  399. used with more than one day's entries displayed.
  400.  
  401. Diary entries can be based on Lisp sexps.  For example, the diary entry
  402.  
  403.       %%(diary-block 11 1 1990 11 10 1990) Vacation
  404.  
  405. causes the diary entry \"Vacation\" to appear from November 1 through November
  406. 10, 1990.  Other functions available are `diary-float', `diary-anniversary',
  407. `diary-cyclic', `diary-day-of-year', `diary-iso-date', `diary-french-date',
  408. `diary-hebrew-date', `diary-islamic-date', `diary-mayan-date',
  409. `diary-yahrzeit', `diary-sunrise-sunset', `diary-phases-of-moon',
  410. `diary-parasha', `diary-omer', `diary-rosh-hodesh', and
  411. `diary-sabbath-candles'.  See the documentation for the function
  412. `list-sexp-diary-entries' for more details.
  413.  
  414. Diary entries based on the Hebrew and/or the Islamic calendar are also
  415. possible, but because these are somewhat slow, they are ignored
  416. unless you set the `nongregorian-diary-listing-hook' and the
  417. `nongregorian-diary-marking-hook' appropriately.  See the documentation
  418. for these functions for details.
  419.  
  420. Diary files can contain directives to include the contents of other files; for
  421. details, see the documentation for the variable `list-diary-entries-hook'."
  422.   :type 'file
  423.   :group 'diary)
  424.  
  425. ;;;###autoload
  426. (defcustom diary-nonmarking-symbol "&"
  427.   "*Symbol indicating that a diary entry is not to be marked in the calendar."
  428.   :type 'string
  429.   :group 'diary)
  430.  
  431. ;;;###autoload
  432. (defcustom hebrew-diary-entry-symbol "H"
  433.   "*Symbol indicating a diary entry according to the Hebrew calendar."
  434.   :type 'string
  435.   :group 'diary)
  436.  
  437. ;;;###autoload
  438. (defcustom islamic-diary-entry-symbol "I"
  439.   "*Symbol indicating a diary entry according to the Islamic calendar."
  440.   :type 'string
  441.   :group 'diary)
  442.  
  443. ;;;###autoload
  444. (defcustom diary-include-string "#include"
  445.   "*The string indicating inclusion of another file of diary entries.
  446. See the documentation for the function `include-other-diary-files'."
  447.   :type 'string
  448.   :group 'diary)
  449.  
  450. ;;;###autoload
  451. (defcustom sexp-diary-entry-symbol "%%"
  452.   "*The string used to indicate a sexp diary entry in diary-file.
  453. See the documentation for the function `list-sexp-diary-entries'."
  454.   :type 'string
  455.   :group 'diary)
  456.  
  457. ;;;###autoload
  458. (defcustom abbreviated-calendar-year t
  459.   "*Interpret a two-digit year DD in a diary entry as either 19DD or 20DD.
  460. For the Gregorian calendar; similarly for the Hebrew and Islamic calendars.
  461. If this variable is nil, years must be written in full."
  462.   :type 'boolean
  463.   :group 'diary)
  464.  
  465. ;;;###autoload
  466. (defcustom european-calendar-style nil
  467.   "*Use the European style of dates in the diary and in any displays.
  468. If this variable is t, a date 1/2/1990 would be interpreted as February 1,
  469. 1990.  The accepted European date styles are
  470.  
  471.             DAY/MONTH
  472.             DAY/MONTH/YEAR
  473.             DAY MONTHNAME
  474.             DAY MONTHNAME YEAR
  475.             DAYNAME
  476.  
  477. Names can be capitalized or not, written in full, or abbreviated to three
  478. characters with or without a period."
  479.   :type 'boolean
  480.   :group 'diary)
  481.  
  482. ;;;###autoload
  483. (defcustom american-date-diary-pattern
  484.   '((month "/" day "[^/0-9]")
  485.     (month "/" day "/" year "[^0-9]")
  486.     (monthname " *" day "[^,0-9]")
  487.     (monthname " *" day ", *" year "[^0-9]")
  488.     (dayname "\\W"))
  489.   "*List of pseudo-patterns describing the American patterns of date used.
  490. See the documentation of `diary-date-forms' for an explanation."
  491.   :type '(repeat (choice (cons :tag "Backup"
  492.                    (const backup)
  493.                    (repeat (list :inline t :format "%v"
  494.                          (symbol :tag "Keyword")
  495.                          (choice symbol regexp))))
  496.              (repeat (list :inline t :format "%v"
  497.                        (symbol :tag "Keyword")
  498.                        (choice symbol regexp)))))
  499.   :group 'diary)
  500.  
  501. ;;;###autoload
  502. (defcustom european-date-diary-pattern
  503.   '((day "/" month "[^/0-9]")
  504.     (day "/" month "/" year "[^0-9]")
  505.     (backup day " *" monthname "\\W+\\<[^*0-9]")
  506.     (day " *" monthname " *" year "[^0-9]")
  507.     (dayname "\\W"))
  508.   "*List of pseudo-patterns describing the European patterns of date used.
  509. See the documentation of `diary-date-forms' for an explanation."
  510.   :type '(repeat (choice (cons :tag "Backup"
  511.                    (const backup)
  512.                    (repeat (list :inline t :format "%v"
  513.                          (symbol :tag "Keyword")
  514.                          (choice symbol regexp))))
  515.              (repeat (list :inline t :format "%v"
  516.                        (symbol :tag "Keyword")
  517.                        (choice symbol regexp)))))
  518.   :group 'diary)
  519.  
  520. (defcustom diary-date-forms
  521.   (if european-calendar-style
  522.       european-date-diary-pattern
  523.     american-date-diary-pattern)
  524.   "*List of pseudo-patterns describing the forms of date used in the diary.
  525. The patterns on the list must be MUTUALLY EXCLUSIVE and must should not match
  526. any portion of the diary entry itself, just the date component.
  527.  
  528. A pseudo-pattern is a list of regular expressions and the keywords `month',
  529. `day', `year', `monthname', and `dayname'.  The keyword `monthname' will
  530. match the name of the month, capitalized or not, or its three-letter
  531. abbreviation, followed by a period or not; it will also match `*'.
  532. Similarly, `dayname' will match the name of the day, capitalized or not, or
  533. its three-letter abbreviation, followed by a period or not.  The keywords
  534. `month', `day', and `year' will match those numerical values, preceded by
  535. arbitrarily many zeros; they will also match `*'.
  536.  
  537. The matching of the diary entries with the date forms is done with the
  538. standard syntax table from Fundamental mode, but with the `*' changed so
  539. that it is a word constituent.
  540.  
  541. If, to be mutually exclusive, a pseudo-pattern must match a portion of the
  542. diary entry itself, the first element of the pattern MUST be `backup'.  This
  543. directive causes the date recognizer to back up to the beginning of the
  544. current word of the diary entry, so in no case can the pattern match more than
  545. a portion of the first word of the diary entry."
  546.   :type '(repeat (choice (cons :tag "Backup"
  547.                    (const backup)
  548.                    (repeat (list :inline t :format "%v"
  549.                          (symbol :tag "Keyword")
  550.                          (choice symbol regexp))))
  551.              (repeat (list :inline t :format "%v"
  552.                        (symbol :tag "Keyword")
  553.                        (choice symbol regexp)))))
  554.   :group 'diary)
  555.  
  556. ;;;###autoload
  557. (defcustom european-calendar-display-form
  558.   '((if dayname (concat dayname ", ")) day " " monthname " " year)
  559.   "*Pseudo-pattern governing the way a date appears in the European style.
  560. See the documentation of calendar-date-display-form for an explanation."
  561.   :type 'sexp
  562.   :group 'calendar)
  563.  
  564. ;;;###autoload
  565. (defcustom american-calendar-display-form
  566.   '((if dayname (concat dayname ", ")) monthname " " day ", " year)
  567.   "*Pseudo-pattern governing the way a date appears in the American style.
  568. See the documentation of `calendar-date-display-form' for an explanation."
  569.   :type 'sexp
  570.   :group 'calendar)
  571.  
  572. (defcustom calendar-date-display-form
  573.   (if european-calendar-style
  574.       european-calendar-display-form
  575.     american-calendar-display-form)
  576.   "*Pseudo-pattern governing the way a date appears.
  577.  
  578. Used by the function `calendar-date-string', a pseudo-pattern is a list of
  579. expressions that can involve the keywords `month', `day', and `year', all
  580. numbers in string form, and `monthname' and `dayname', both alphabetic
  581. strings.  For example, the ISO standard would use the pseudo- pattern
  582.  
  583.        '(year \"-\" month \"-\" day)
  584.  
  585. while a typical American form would be
  586.  
  587.        '(month \"/\" day \"/\" (substring year -2))
  588.  
  589. and
  590.  
  591.        '((format \"%9s, %9s %2s, %4s\" dayname monthname day year))
  592.  
  593. would give the usual American style in fixed-length fields.
  594.  
  595. See the documentation of the function `calendar-date-string'."
  596.   :type 'sexp
  597.   :group 'calendar)
  598.  
  599. (defun european-calendar ()
  600.   "Set the interpretation and display of dates to the European style."
  601.   (interactive)
  602.   (setq european-calendar-style t)
  603.   (setq calendar-date-display-form european-calendar-display-form)
  604.   (setq diary-date-forms european-date-diary-pattern)
  605.   (update-calendar-mode-line))
  606.  
  607. (defun american-calendar ()
  608.   "Set the interpretation and display of dates to the American style."
  609.   (interactive)
  610.   (setq european-calendar-style nil)
  611.   (setq calendar-date-display-form american-calendar-display-form)
  612.   (setq diary-date-forms american-date-diary-pattern)
  613.   (update-calendar-mode-line))
  614.  
  615. ;;;###autoload
  616. (defcustom print-diary-entries-hook 'lpr-buffer
  617.   "*List of functions called after a temporary diary buffer is prepared.
  618. The buffer shows only the diary entries currently visible in the diary
  619. buffer.  The default just does the printing.  Other uses might include, for
  620. example, rearranging the lines into order by day and time, saving the buffer
  621. instead of deleting it, or changing the function used to do the printing."
  622.   :type 'hook
  623.   :group 'diary)
  624.  
  625. ;;;###autoload
  626. (defcustom list-diary-entries-hook nil
  627.   "*List of functions called after diary file is culled for relevant entries.
  628. It is to be used for diary entries that are not found in the diary file.
  629.  
  630. A function `include-other-diary-files' is provided for use as the value of
  631. this hook.  This function enables you to use shared diary files together
  632. with your own.  The files included are specified in the diary file by lines
  633. of the form
  634.  
  635.         #include \"filename\"
  636.  
  637. This is recursive; that is, #include directives in files thus included are
  638. obeyed.  You can change the \"#include\" to some other string by changing
  639. the variable `diary-include-string'.  When you use `include-other-diary-files'
  640. as part of the list-diary-entries-hook, you will probably also want to use the
  641. function `mark-included-diary-files' as part of `mark-diary-entries-hook'.
  642.  
  643. For example, you could use
  644.  
  645.      (setq list-diary-entries-hook
  646.        '(include-other-diary-files sort-diary-entries))
  647.      (setq diary-display-hook 'fancy-diary-display)
  648.  
  649. in your `.emacs' file to cause the fancy diary buffer to be displayed with
  650. diary entries from various included files, each day's entries sorted into
  651. lexicographic order."
  652.   :type 'hook
  653.   :group 'diary)
  654.  
  655. ;;;###autoload
  656. (defcustom diary-hook nil
  657.   "*List of functions called after the display of the diary.
  658. Can be used for appointment notification."
  659.   :type 'hook
  660.   :group 'diary)
  661.  
  662. ;;;###autoload
  663. (defcustom diary-display-hook nil
  664.   "*List of functions that handle the display of the diary.
  665. If nil (the default), `simple-diary-display' is used.  Use `ignore' for no
  666. diary display.
  667.  
  668. Ordinarily, this just displays the diary buffer (with holidays indicated in
  669. the mode line), if there are any relevant entries.  At the time these
  670. functions are called, the variable `diary-entries-list' is a list, in order
  671. by date, of all relevant diary entries in the form of ((MONTH DAY YEAR)
  672. STRING), where string is the diary entry for the given date.  This can be
  673. used, for example, a different buffer for display (perhaps combined with
  674. holidays), or produce hard copy output.
  675.  
  676. A function `fancy-diary-display' is provided as an alternative
  677. choice for this hook; this function prepares a special noneditable diary
  678. buffer with the relevant diary entries that has neat day-by-day arrangement
  679. with headings.  The fancy diary buffer will show the holidays unless the
  680. variable `holidays-in-diary-buffer' is set to nil.  Ordinarily, the fancy
  681. diary buffer will not show days for which there are no diary entries, even
  682. if that day is a holiday; if you want such days to be shown in the fancy
  683. diary buffer, set the variable `diary-list-include-blanks' to t."
  684.   :type 'hook
  685.   :group 'diary)
  686.  
  687. ;;;###autoload
  688. (defcustom nongregorian-diary-listing-hook nil
  689.   "*List of functions called for listing diary file and included files.
  690. As the files are processed for diary entries, these functions are used to cull
  691. relevant entries.  You can use either or both of `list-hebrew-diary-entries'
  692. and `list-islamic-diary-entries'.  The documentation for these functions
  693. describes the style of such diary entries."
  694.   :type 'hook
  695.   :group 'diary)
  696.  
  697. ;;;###autoload
  698. (defcustom mark-diary-entries-hook nil
  699.   "*List of functions called after marking diary entries in the calendar.
  700.  
  701. A function `mark-included-diary-files' is also provided for use as the
  702. mark-diary-entries-hook; it enables you to use shared diary files together
  703. with your own.  The files included are specified in the diary file by lines
  704. of the form
  705.         #include \"filename\"
  706. This is recursive; that is, #include directives in files thus included are
  707. obeyed.  You can change the \"#include\" to some other string by changing the
  708. variable `diary-include-string'.  When you use `mark-included-diary-files' as
  709. part of the mark-diary-entries-hook, you will probably also want to use the
  710. function `include-other-diary-files' as part of `list-diary-entries-hook'."
  711.   :type 'hook
  712.   :group 'diary)
  713.  
  714. ;;;###autoload
  715. (defcustom nongregorian-diary-marking-hook nil
  716.   "*List of functions called for marking diary file and included files.
  717. As the files are processed for diary entries, these functions are used to cull
  718. relevant entries.  You can use either or both of `mark-hebrew-diary-entries'
  719. and `mark-islamic-diary-entries'.  The documentation for these functions
  720. describes the style of such diary entries."
  721.   :type 'hook
  722.   :group 'diary)
  723.  
  724. ;;;###autoload
  725. (defcustom diary-list-include-blanks nil
  726.   "*If nil, do not include days with no diary entry in the list of diary entries.
  727. Such days will then not be shown in the fancy diary buffer, even if they
  728. are holidays."
  729.   :type 'boolean
  730.   :group 'diary)
  731.  
  732. ;;;###autoload
  733. (defcustom holidays-in-diary-buffer t
  734.   "*Non-nil means include holidays in the diary display.
  735. The holidays appear in the mode line of the diary buffer, or in the
  736. fancy diary buffer next to the date.  This slows down the diary functions
  737. somewhat; setting it to nil makes the diary display faster."
  738.   :type 'boolean
  739.   :group 'diary)
  740.  
  741. (defvar calendar-mark-ring nil)
  742.  
  743. ;;;###autoload
  744. (defcustom general-holidays
  745.   '((holiday-fixed 1 1 "New Year's Day")
  746.     (holiday-float 1 1 3 "Martin Luther King Day")
  747.     (holiday-fixed 2 2 "Ground Hog Day")
  748.     (holiday-fixed 2 14 "Valentine's Day")
  749.     (holiday-float 2 1 3 "President's Day")
  750.     (holiday-fixed 3 17 "St. Patrick's Day")
  751.     (holiday-fixed 4 1 "April Fool's Day")
  752.     (holiday-float 5 0 2 "Mother's Day")
  753.     (holiday-float 5 1 -1 "Memorial Day")
  754.     (holiday-fixed 6 14 "Flag Day")
  755.     (holiday-float 6 0 3 "Father's Day")
  756.     (holiday-fixed 7 4 "Independence Day")
  757.     (holiday-float 9 1 1 "Labor Day")
  758.     (holiday-float 10 1 2 "Columbus Day")
  759.     (holiday-fixed 10 31 "Halloween")
  760.     (holiday-fixed 11 11 "Veteran's Day")
  761.     (holiday-float 11 4 4 "Thanksgiving"))
  762.   "*General holidays.  Default value is for the United States.
  763. See the documentation for `calendar-holidays' for details."
  764.   :type 'sexp
  765.   :group 'holidays)
  766.  
  767. ;;;###autoload
  768. (put 'general-holidays 'risky-local-variable t)
  769.  
  770. ;;;###autoload
  771. (defcustom local-holidays nil
  772.   "*Local holidays.
  773. See the documentation for `calendar-holidays' for details."
  774.   :type 'sexp
  775.   :group 'holidays
  776.   :group 'local)
  777.  
  778. ;;;###autoload
  779. (put 'local-holidays 'risky-local-variable t)
  780. ;;;###autoload
  781. (defcustom other-holidays nil
  782.   "*User defined holidays.
  783. See the documentation for `calendar-holidays' for details."
  784.   :type 'sexp
  785.   :group 'holidays)
  786.  
  787. ;;;###autoload
  788. (put 'other-holidays 'risky-local-variable t)
  789.  
  790. ;;;###autoload
  791. (defvar hebrew-holidays-1
  792.   '((holiday-rosh-hashanah-etc)
  793.     (if all-hebrew-calendar-holidays
  794.         (holiday-julian
  795.          11
  796.          (let* ((m displayed-month)
  797.                 (y displayed-year)
  798.                 (year))
  799.            (increment-calendar-month m y -1)
  800.            (let ((year (extract-calendar-year
  801.                         (calendar-julian-from-absolute
  802.                          (calendar-absolute-from-gregorian
  803.                           (list m 1 y))))))
  804.              (if (zerop (% (1+ year) 4))
  805.                  22
  806.                21))) "\"Tal Umatar\" (evening)"))))
  807.  
  808. ;;;###autoload
  809. (put 'hebrew-holidays-1 'risky-local-variable t)
  810.  
  811. ;;;###autoload
  812. (defvar hebrew-holidays-2
  813.   '((if all-hebrew-calendar-holidays
  814.         (holiday-hanukkah)
  815.       (holiday-hebrew 9 25 "Hanukkah"))
  816.     (if all-hebrew-calendar-holidays
  817.       (holiday-hebrew
  818.        10
  819.        (let ((h-year (extract-calendar-year
  820.                       (calendar-hebrew-from-absolute
  821.                        (calendar-absolute-from-gregorian
  822.                         (list displayed-month 28 displayed-year))))))
  823.          (if (= (% (calendar-absolute-from-hebrew (list 10 10 h-year))
  824.                    7)
  825.                 6)
  826.              11 10))
  827.        "Tzom Teveth"))
  828.     (if all-hebrew-calendar-holidays
  829.         (holiday-hebrew 11 15 "Tu B'Shevat"))))
  830.  
  831. ;;;###autoload
  832. (put 'hebrew-holidays-2 'risky-local-variable t)
  833.  
  834. ;;;###autoload
  835. (defvar hebrew-holidays-3
  836.   '((if all-hebrew-calendar-holidays
  837.         (holiday-hebrew
  838.          11
  839.          (let ((m displayed-month)
  840.                (y displayed-year))
  841.            (increment-calendar-month m y 1)
  842.            (let* ((h-year (extract-calendar-year
  843.                            (calendar-hebrew-from-absolute
  844.                             (calendar-absolute-from-gregorian
  845.                              (list m
  846.                                    (calendar-last-day-of-month m y)
  847.                                    y)))))
  848.                   (s-s
  849.                    (calendar-hebrew-from-absolute
  850.                     (if (=
  851.                          (% (calendar-absolute-from-hebrew
  852.                              (list 7 1 h-year))
  853.                             7)
  854.                          6)
  855.                         (calendar-dayname-on-or-before
  856.                          6 (calendar-absolute-from-hebrew
  857.                             (list 11 17 h-year)))
  858.                       (calendar-dayname-on-or-before
  859.                        6 (calendar-absolute-from-hebrew
  860.                           (list 11 16 h-year))))))
  861.                   (day (extract-calendar-day s-s)))
  862.              day))
  863.          "Shabbat Shirah"))))
  864. ;;;###autoload
  865. (put 'hebrew-holidays-3 'risky-local-variable t)
  866.  
  867. ;;;###autoload
  868. (defvar hebrew-holidays-4
  869.   '((holiday-passover-etc)
  870.     (if (and all-hebrew-calendar-holidays
  871.              (let* ((m displayed-month)
  872.                     (y displayed-year)
  873.                     (year))
  874.                (increment-calendar-month m y -1)
  875.                (let ((year (extract-calendar-year
  876.                             (calendar-julian-from-absolute
  877.                              (calendar-absolute-from-gregorian
  878.                               (list m 1 y))))))
  879.                  (= 21 (% year 28)))))
  880.         (holiday-julian 3 26 "Kiddush HaHamah"))
  881.     (if all-hebrew-calendar-holidays
  882.         (holiday-tisha-b-av-etc))))
  883. ;;;###autoload
  884. (put 'hebrew-holidays-4 'risky-local-variable t)
  885.  
  886. ;;;###autoload
  887. (defvar hebrew-holidays (append hebrew-holidays-1 hebrew-holidays-2
  888.                 hebrew-holidays-3 hebrew-holidays-4)
  889.   "*Jewish holidays.
  890. See the documentation for `calendar-holidays' for details.")
  891.  
  892. ;;;###autoload
  893. (put 'hebrew-holidays 'risky-local-variable t)
  894.  
  895. ;;;###autoload
  896. (defvar christian-holidays
  897.   '((if all-christian-calendar-holidays
  898.         (holiday-fixed 1 6 "Epiphany"))
  899.     (holiday-easter-etc)
  900.     (if all-christian-calendar-holidays
  901.         (holiday-greek-orthodox-easter))
  902.     (if all-christian-calendar-holidays
  903.         (holiday-fixed 8 15 "Assumption"))
  904.     (if all-christian-calendar-holidays
  905.         (holiday-advent))
  906.     (holiday-fixed 12 25 "Christmas")
  907.     (if all-christian-calendar-holidays
  908.         (holiday-julian 12 25 "Eastern Orthodox Christmas")))
  909.   "*Christian holidays.
  910. See the documentation for `calendar-holidays' for details.")
  911.  
  912. ;;;###autoload
  913. (put 'christian-holidays 'risky-local-variable t)
  914.  
  915. ;;;###autoload
  916. (defvar islamic-holidays
  917.   '((holiday-islamic
  918.      1 1
  919.      (format "Islamic New Year %d"
  920.              (let ((m displayed-month)
  921.                    (y displayed-year))
  922.                (increment-calendar-month m y 1)
  923.                (extract-calendar-year
  924.                 (calendar-islamic-from-absolute
  925.                  (calendar-absolute-from-gregorian
  926.                   (list
  927.                    m (calendar-last-day-of-month m y) y)))))))
  928.     (if all-islamic-calendar-holidays
  929.         (holiday-islamic 1 10 "Ashura"))
  930.     (if all-islamic-calendar-holidays
  931.         (holiday-islamic 3 12 "Mulad-al-Nabi"))
  932.     (if all-islamic-calendar-holidays
  933.         (holiday-islamic 7 26 "Shab-e-Mi'raj"))
  934.     (if all-islamic-calendar-holidays
  935.         (holiday-islamic 8 15 "Shab-e-Bara't"))
  936.     (holiday-islamic 9 1 "Ramadan Begins")
  937.     (if all-islamic-calendar-holidays
  938.         (holiday-islamic 9 27 "Shab-e Qadr"))
  939.     (if all-islamic-calendar-holidays
  940.         (holiday-islamic 10 1 "Id-al-Fitr"))
  941.     (if all-islamic-calendar-holidays
  942.         (holiday-islamic 12 10 "Id-al-Adha")))
  943.   "*Islamic holidays.
  944. See the documentation for `calendar-holidays' for details.")
  945.  
  946. ;;;###autoload
  947. (put 'islamic-holidays 'risky-local-variable t)
  948.  
  949. ;;;###autoload
  950. (defvar solar-holidays
  951.   '((if (fboundp 'atan)
  952.     (solar-equinoxes-solstices))
  953.     (if (progn
  954.       (require 'cal-dst)
  955.       t)
  956.       (funcall
  957.        'holiday-sexp
  958.         calendar-daylight-savings-starts
  959.         '(format "Daylight Savings Time Begins %s"
  960.                   (if (fboundp 'atan)
  961.                       (solar-time-string
  962.                        (/ calendar-daylight-savings-starts-time (float 60))
  963.                        calendar-standard-time-zone-name)
  964.                     ""))))
  965.     (funcall
  966.      'holiday-sexp
  967.      calendar-daylight-savings-ends
  968.      '(format "Daylight Savings Time Ends %s"
  969.               (if (fboundp 'atan)
  970.                   (solar-time-string
  971.                    (/ calendar-daylight-savings-ends-time (float 60))
  972.                    calendar-daylight-time-zone-name)
  973.                 ""))))
  974.   "*Sun-related holidays.
  975. See the documentation for `calendar-holidays' for details.")
  976.  
  977. ;;;###autoload
  978. (put 'solar-holidays 'risky-local-variable t)
  979.  
  980. ;;;###autoload
  981. (defvar calendar-holidays
  982.   (append general-holidays local-holidays other-holidays
  983.           christian-holidays hebrew-holidays islamic-holidays
  984.           solar-holidays)
  985.   "*List of notable days for the command M-x holidays.
  986.  
  987. Additional holidays are easy to add to the list, just put them in the list
  988. `other-holidays' in your .emacs file.  Similarly, by setting any of
  989. `general-holidays', `local-holidays' `christian-holidays', `hebrew-holidays',
  990. `islamic-holidays', or `solar-holidays' to nil in your .emacs file, you can
  991. eliminate unwanted categories of holidays.  The intention is that (in the US)
  992. `local-holidays' be set in site-init.el and `other-holidays' be set by the
  993. user.
  994.  
  995. Entries on the list are expressions that return (possibly empty) lists of
  996. items of the form ((month day year) string) of a holiday in the in the
  997. three-month period centered around `displayed-month' of `displayed-year'.
  998. Several basic functions are provided for this purpose:
  999.  
  1000.     (holiday-fixed MONTH DAY STRING) is a fixed date on the Gregorian calendar
  1001.     (holiday-float MONTH DAYNAME K STRING &optional day) is the Kth DAYNAME in
  1002.                                MONTH on the Gregorian calendar (0 for Sunday,
  1003.                                etc.); K<0 means count back from the end of the
  1004.                                month. An optional parameter DAY means the Kth
  1005.                                DAYNAME after/before MONTH DAY.
  1006.     (holiday-hebrew MONTH DAY STRING)  a fixed date on the Hebrew calendar
  1007.     (holiday-islamic MONTH DAY STRING) a fixed date on the Islamic calendar
  1008.     (holiday-julian MONTH DAY STRING)  a fixed date on the Julian calendar
  1009.     (holiday-sexp SEXP STRING) SEXP is a Gregorian-date-valued expression
  1010.                                in the variable `year'; if it evaluates to
  1011.                                a visible date, that's the holiday; if it
  1012.                                evaluates to nil, there's no holiday.  STRING
  1013.                                is an expression in the variable `date'.
  1014.  
  1015. For example, to add Bastille Day, celebrated in France on July 14, add
  1016.  
  1017.      (holiday-fixed 7 14 \"Bastille Day\")
  1018.  
  1019. to the list.  To add Hurricane Supplication Day, celebrated in the Virgin
  1020. Islands on the fourth Monday in August, add
  1021.  
  1022.      (holiday-float 8 1 4 \"Hurricane Supplication Day\")
  1023.  
  1024. to the list (the last Monday would be specified with `-1' instead of `4').
  1025. To add the last day of Hanukkah to the list, use
  1026.  
  1027.      (holiday-hebrew 10 2 \"Last day of Hanukkah\")
  1028.  
  1029. since the Hebrew months are numbered with 1 starting from Nisan, while to
  1030. add the Islamic feast celebrating Mohammed's birthday use
  1031.  
  1032.      (holiday-islamic 3 12 \"Mohammed's Birthday\")
  1033.  
  1034. since the Islamic months are numbered from 1 starting with Muharram.  To
  1035. add Thomas Jefferson's birthday, April 2, 1743 (Julian), use
  1036.  
  1037.      (holiday-julian 4 2 \"Jefferson's Birthday\")
  1038.  
  1039. To include a holiday conditionally, use the sexp form or a conditional.  For
  1040. example, to include American presidential elections, which occur on the first
  1041. Tuesday after the first Monday in November of years divisible by 4, add
  1042.  
  1043.      (holiday-sexp
  1044.        (if (zerop (% year 4))
  1045.            (calendar-gregorian-from-absolute
  1046.              (1+ (calendar-dayname-on-or-before
  1047.                    1 (+ 6 (calendar-absolute-from-gregorian
  1048.                             (list 11 1 year)))))))
  1049.        \"US Presidential Election\")
  1050.  
  1051. or
  1052.  
  1053.      (if (zerop (% displayed-year 4))
  1054.          (holiday-fixed 11
  1055.                 (extract-calendar-day
  1056.                  (calendar-gregorian-from-absolute
  1057.                   (1+ (calendar-dayname-on-or-before
  1058.                        1 (+ 6 (calendar-absolute-from-gregorian
  1059.                                (list 11 1 displayed-year)))))))
  1060.                 \"US Presidential Election\"))
  1061.  
  1062. to the list.  To include the phases of the moon, add
  1063.  
  1064.      (lunar-phases)
  1065.  
  1066. to the holiday list, where `lunar-phases' is an Emacs-Lisp function that
  1067. you've written to return a (possibly empty) list of the relevant VISIBLE dates
  1068. with descriptive strings such as
  1069.  
  1070.      (((2 6 1989) \"New Moon\") ((2 12 1989) \"First Quarter Moon\") ... ).")
  1071.  
  1072. ;;;###autoload
  1073. (put 'calendar-holidays 'risky-local-variable t)
  1074.  
  1075. (defconst calendar-buffer "*Calendar*"
  1076.   "Name of the buffer used for the calendar.")
  1077.  
  1078. (defconst holiday-buffer "*Holidays*"
  1079.   "Name of the buffer used for the displaying the holidays.")
  1080.  
  1081. (defconst fancy-diary-buffer "*Fancy Diary Entries*"
  1082.   "Name of the buffer used for the optional fancy display of the diary.")
  1083.  
  1084. (defconst lunar-phases-buffer "*Phases of Moon*"
  1085.   "Name of the buffer used for the lunar phases.")
  1086.  
  1087. (defmacro increment-calendar-month (mon yr n)
  1088.   "Move the variables MON and YR to the month and year by N months.
  1089. Forward if N is positive or backward if N is negative."
  1090.   (` (let (( macro-y (+ (* (, yr) 12) (, mon) -1 (, n) )))
  1091.        (setq (, mon) (1+ (% macro-y 12) ))
  1092.        (setq (, yr) (/ macro-y 12)))))
  1093.  
  1094. (defmacro calendar-for-loop (var from init to final do &rest body)
  1095.   "Execute a for loop."
  1096.   (` (let (( (, var) (1- (, init)) ))
  1097.        (while (>= (, final) (setq (, var) (1+ (, var))))
  1098.          (,@ body)))))
  1099.  
  1100. (defmacro calendar-sum (index initial condition expression)
  1101.   "For INDEX = INITIAL et seq, as long as CONDITION holds, sum EXPRESSION."
  1102.   (` (let (( (, index) (, initial))
  1103.              (sum 0))
  1104.        (while (, condition)
  1105.          (setq sum (+ sum (, expression) ))
  1106.          (setq (, index) (1+ (, index))))
  1107.        sum)))
  1108.  
  1109. ;; The following are in-line for speed; they can be called thousands of times
  1110. ;; when looking up holidays or processing the diary.  Here, for example, are
  1111. ;; the numbers of calls to calendar/diary/holiday functions in preparing the
  1112. ;; fancy diary display, for a moderately complex diary file, with functions
  1113. ;; used instead of macros.  There were a total of 10000 such calls:
  1114. ;;
  1115. ;;  1934   extract-calendar-month
  1116. ;;  1852   extract-calendar-year
  1117. ;;  1819   extract-calendar-day
  1118. ;;   845   calendar-leap-year-p
  1119. ;;   837   calendar-day-number
  1120. ;;   775   calendar-absolute-from-gregorian
  1121. ;;   346   calendar-last-day-of-month
  1122. ;;   286   hebrew-calendar-last-day-of-month
  1123. ;;   188   hebrew-calendar-leap-year-p
  1124. ;;   180   hebrew-calendar-elapsed-days
  1125. ;;   163   hebrew-calendar-last-month-of-year
  1126. ;;    66   calendar-date-compare
  1127. ;;    65   hebrew-calendar-days-in-year
  1128. ;;    60   calendar-absolute-from-julian
  1129. ;;    50   calendar-absolute-from-hebrew
  1130. ;;    43   calendar-date-equal
  1131. ;;    38   calendar-gregorian-from-absolute
  1132. ;;     .
  1133. ;;     .
  1134. ;;     .
  1135. ;;
  1136. ;; The use of these seven macros eliminates the overhead of 92% of the function
  1137. ;; calls; it's faster this way.
  1138.  
  1139. (defsubst extract-calendar-month (date)
  1140.   "Extract the month part of DATE which has the form (month day year)."
  1141.   (car date))
  1142.  
  1143. (defsubst extract-calendar-day (date)
  1144.   "Extract the day part of DATE which has the form (month day year)."
  1145.   (car (cdr date)))
  1146.  
  1147. (defsubst extract-calendar-year (date)
  1148.   "Extract the year part of DATE which has the form (month day year)."
  1149.   (car (cdr (cdr date))))
  1150.  
  1151. (defsubst calendar-leap-year-p (year)
  1152.   "Returns t if YEAR is a Gregorian leap year."
  1153.   (and (zerop (% year 4))
  1154.        (or (not (zerop (% year 100)))
  1155.            (zerop (% year 400)))))
  1156.  
  1157. ;; The foregoing is a bit faster, but not as clear as the following:
  1158. ;;
  1159. ;;(defsubst calendar-leap-year-p (year)
  1160. ;;  "Returns t if YEAR is a Gregorian leap year."
  1161. ;;  (or
  1162. ;;    (and (=  (% year   4) 0)
  1163. ;;         (/= (% year 100) 0))
  1164. ;;    (= (% year 400) 0)))
  1165.  
  1166. (defsubst calendar-last-day-of-month (month year)
  1167.   "The last day in MONTH during YEAR."
  1168.   (if (and (= month 2) (calendar-leap-year-p year))
  1169.       29
  1170.     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  1171.  
  1172. ;; An explanation of the calculation can be found in PascAlgorithms by
  1173. ;; Edward and Ruth Reingold, Scott-Foresman/Little, Brown, 1988.
  1174.  
  1175. (defsubst calendar-day-number (date)
  1176.   "Return the day number within the year of the date DATE.
  1177. For example, (calendar-day-number '(1 1 1987)) returns the value 1,
  1178. while (calendar-day-number '(12 31 1980)) returns 366."
  1179.     (let* ((month (extract-calendar-month date))
  1180.            (day (extract-calendar-day date))
  1181.            (year (extract-calendar-year date))
  1182.          (day-of-year (+ day (* 31 (1- month)))))
  1183.       (if (> month 2)
  1184.           (progn
  1185.             (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  1186.             (if (calendar-leap-year-p year)
  1187.                 (setq day-of-year (1+ day-of-year)))))
  1188.       day-of-year))
  1189.  
  1190. (defsubst calendar-absolute-from-gregorian (date)
  1191.   "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  1192. The Gregorian date Sunday, December 31, 1 BC is imaginary."
  1193.   (let ((prior-years (1- (extract-calendar-year date))))
  1194.     (+ (calendar-day-number date);; Days this year
  1195.        (* 365 prior-years);;        + Days in prior years
  1196.        (/ prior-years 4);;          + Julian leap years
  1197.        (- (/ prior-years 100));;    - century years
  1198.        (/ prior-years 400))));;     + Gregorian leap years
  1199.  
  1200. ;;;###autoload
  1201. (defun calendar (&optional arg)
  1202.   "Display a three-month calendar in another window.
  1203. The three months appear side by side, with the current month in the middle
  1204. surrounded by the previous and next months.  The cursor is put on today's date.
  1205.  
  1206. If called with an optional prefix argument, prompts for month and year.
  1207.  
  1208. This function is suitable for execution in a .emacs file; appropriate setting
  1209. of the variable `view-diary-entries-initially' will cause the diary entries for
  1210. the current date to be displayed in another window.  The value of the variable
  1211. `number-of-diary-entries' controls the number of days of diary entries
  1212. displayed upon initial display of the calendar.
  1213.  
  1214. An optional prefix argument ARG causes the calendar displayed to be ARG
  1215. months in the future if ARG is positive or in the past if ARG is negative;
  1216. in this case the cursor goes on the first day of the month.
  1217.  
  1218. Once in the calendar window, future or past months can be moved into view.
  1219. Arbitrary months can be displayed, or the calendar can be scrolled forward
  1220. or backward.
  1221.  
  1222. The cursor can be moved forward or backward by one day, one week, one month,
  1223. or one year.  All of these commands take prefix arguments which, when negative,
  1224. cause movement in the opposite direction.  For convenience, the digit keys
  1225. and the minus sign are automatically prefixes.  The window is replotted as
  1226. necessary to display the desired date.
  1227.  
  1228. Diary entries can be marked on the calendar or displayed in another window.
  1229.  
  1230. Use M-x describe-mode for details of the key bindings in the calendar window.
  1231.  
  1232. The Gregorian calendar is assumed.
  1233.  
  1234. After loading the calendar, the hooks given by the variable
  1235. `calendar-load-hook' are run.  This is the place to add key bindings to the
  1236. calendar-mode-map.
  1237.  
  1238. After preparing the calendar window initially, the hooks given by the variable
  1239. `initial-calendar-window-hook' are run.
  1240.  
  1241. The hooks given by the variable `today-visible-calendar-hook' are run
  1242. everytime the calendar window gets scrolled, if the current date is visible
  1243. in the window.  If it is not visible, the hooks given by the variable
  1244. `today-invisible-calendar-hook' are run.  Thus, for example, setting
  1245. `today-visible-calendar-hook' to 'calendar-star-date will cause today's date
  1246. to be replaced by asterisks to highlight it whenever it is in the window."
  1247.   (interactive "P")
  1248.   (set-buffer (get-buffer-create calendar-buffer))
  1249.   (calendar-mode)
  1250.   (let* ((pop-up-windows t)
  1251.          (split-height-threshold 1000)
  1252.          (date (if arg
  1253.                    (calendar-read-date t)
  1254.                  (calendar-current-date)))
  1255.          (month (extract-calendar-month date))
  1256.          (year (extract-calendar-year date)))
  1257.     (pop-to-buffer calendar-buffer)
  1258.     (increment-calendar-month month year (- calendar-offset))
  1259.     (generate-calendar-window month year)
  1260.     (if (and view-diary-entries-initially (calendar-date-is-visible-p date))
  1261.         (view-diary-entries
  1262.          (if (vectorp number-of-diary-entries)
  1263.              (aref number-of-diary-entries (calendar-day-of-week date))
  1264.            number-of-diary-entries))))
  1265.   (let* ((diary-buffer (get-file-buffer diary-file))
  1266.          (diary-window (if diary-buffer (get-buffer-window diary-buffer)))
  1267.          (split-height-threshold (if diary-window 2 1000)))
  1268.     (if view-calendar-holidays-initially
  1269.         (list-calendar-holidays)))
  1270.   (run-hooks 'initial-calendar-window-hook))
  1271.  
  1272. (autoload 'view-diary-entries "diary-lib"
  1273.   "Prepare and display a buffer with diary entries.
  1274. Searches your diary file for entries that match ARG days starting with
  1275. the date indicated by the cursor position in the displayed three-month
  1276. calendar."
  1277.   t)
  1278.  
  1279. (autoload 'calendar-sunrise-sunset "solar"
  1280.   "Local time of sunrise and sunset for date under cursor."
  1281.   t)
  1282.  
  1283. (autoload 'calendar-phases-of-moon "lunar"
  1284.   "Create a buffer of the phases of the moon for the current calendar window."
  1285.   t)
  1286.  
  1287. (autoload 'calendar-print-french-date "cal-french"
  1288.   "Show the French Revolutionary calendar equivalent of the date under the cursor."
  1289.   t)
  1290.  
  1291. (autoload 'calendar-goto-french-date "cal-french"
  1292.  "Move cursor to French Revolutionary date."
  1293.   t)
  1294.  
  1295. (autoload 'calendar-french-date-string "cal-french"
  1296.   "String of French Revolutionary date of Gregorian DATE."
  1297.   t)
  1298.  
  1299. (autoload 'calendar-mayan-date-string "cal-mayan"
  1300.   "String of Mayan date of Gregorian DATE."
  1301.   t)
  1302.  
  1303. (autoload 'calendar-print-mayan-date "cal-mayan"
  1304.   "Show the Mayan long count, Tzolkin, and Haab equivalents of the date under the cursor."
  1305.   t)
  1306.  
  1307. (autoload 'calendar-goto-mayan-long-count-date "cal-mayan"
  1308.  "Move cursor to Mayan long count date."
  1309.   t)
  1310.  
  1311. (autoload 'calendar-next-haab-date "cal-mayan"
  1312.   "Move cursor to next instance of Mayan Haab date."
  1313.   t)
  1314.  
  1315. (autoload 'calendar-previous-haab-date "cal-mayan"
  1316.   "Move cursor to previous instance of Mayan Haab date."
  1317.   t)
  1318.  
  1319. (autoload 'calendar-next-tzolkin-date "cal-mayan"
  1320.   "Move cursor to next instance of Mayan Tzolkin date."
  1321.   t)
  1322.  
  1323. (autoload 'calendar-previous-tzolkin-date "cal-mayan"
  1324.   "Move cursor to previous instance of Mayan Tzolkin date."
  1325.   t)
  1326.  
  1327. (autoload 'calendar-next-calendar-round-date "cal-mayan"
  1328.   "Move cursor to next instance of Mayan Haab/Tzoklin combination."
  1329.   t)
  1330.  
  1331. (autoload 'calendar-previous-calendar-round-date "cal-mayan"
  1332.   "Move cursor to previous instance of Mayan Haab/Tzoklin combination."
  1333.   t)
  1334.  
  1335. (autoload 'show-all-diary-entries "diary-lib"
  1336.   "Show all of the diary entries in the diary file.
  1337. This function gets rid of the selective display of the diary file so that
  1338. all entries, not just some, are visible.  If there is no diary buffer, one
  1339. is created."
  1340.   t)
  1341.  
  1342. (autoload 'mark-diary-entries "diary-lib"
  1343.   "Mark days in the calendar window that have diary entries.
  1344. Each entry in diary file visible in the calendar window is marked."
  1345.   t)
  1346.  
  1347. (autoload 'insert-diary-entry "diary-ins"
  1348.   "Insert a diary entry for the date indicated by point."
  1349.   t)
  1350.  
  1351. (autoload 'insert-weekly-diary-entry "diary-ins"
  1352.   "Insert a weekly diary entry for the day of the week indicated by point."
  1353.   t)
  1354.  
  1355.  
  1356. (autoload 'insert-monthly-diary-entry "diary-ins"
  1357.   "Insert a monthly diary entry for the day of the month indicated by point."
  1358.   t)
  1359.  
  1360. (autoload 'insert-yearly-diary-entry "diary-ins"
  1361.   "Insert an annual diary entry for the day of the year indicated by point."
  1362.   t)
  1363.  
  1364. (autoload 'insert-anniversary-diary-entry "diary-ins"
  1365.   "Insert an anniversary diary entry for the date indicated by point."
  1366.   t)
  1367.  
  1368. (autoload 'insert-block-diary-entry "diary-ins"
  1369.   "Insert a block diary entry for the dates indicated by point and mark."
  1370.   t)
  1371.  
  1372. (autoload 'insert-cyclic-diary-entry "diary-ins"
  1373.   "Insert a cyclic diary entry starting at the date indicated by point."
  1374.   t)
  1375.  
  1376. (autoload 'insert-hebrew-diary-entry "diary-ins"
  1377.   "Insert a diary entry for the Hebrew date corresponding to the date
  1378. indicated by point."
  1379.   t)
  1380.  
  1381. (autoload 'insert-monthly-hebrew-diary-entry "diary-ins"
  1382.   "Insert a monthly diary entry for the day of the Hebrew month corresponding
  1383. to the date indicated by point."
  1384.   t)
  1385.  
  1386. (autoload 'insert-yearly-hebrew-diary-entry "diary-ins"
  1387.   "Insert an annual diary entry for the day of the Hebrew year corresponding
  1388. to the date indicated by point."
  1389.   t)
  1390.  
  1391. (autoload 'insert-islamic-diary-entry "diary-ins"
  1392.   "Insert a diary entry for the Islamic date corresponding to the date
  1393. indicated by point."
  1394.   t)
  1395.  
  1396. (autoload 'insert-monthly-islamic-diary-entry "diary-ins"
  1397.   "Insert a monthly diary entry for the day of the Islamic month corresponding
  1398. to the date indicated by point."
  1399.   t)
  1400.  
  1401. (autoload 'insert-yearly-islamic-diary-entry "diary-ins"
  1402.   "Insert an annual diary entry for the day of the Islamic year corresponding
  1403. to the date indicated by point."
  1404.   t)
  1405.  
  1406. (autoload 'list-calendar-holidays "holidays"
  1407.   "Create a buffer containing the holidays for the current calendar window.
  1408. The holidays are those in the list `calendar-notable-days'.  Returns t if any
  1409. holidays are found, nil if not."
  1410.   t)
  1411.  
  1412. (autoload 'mark-calendar-holidays "holidays"
  1413.   "Mark notable days in the calendar window."
  1414.   t)
  1415.  
  1416. (autoload 'calendar-cursor-holidays "holidays"
  1417.   "Find holidays for the date specified by the cursor in the calendar window."
  1418.   t)
  1419.  
  1420. (defun generate-calendar-window (&optional mon yr)
  1421.   "Generate the calendar window for the current date.
  1422. Or, for optional MON, YR."
  1423.   (let* ((buffer-read-only nil)
  1424.          (today (calendar-current-date))
  1425.          (month (extract-calendar-month today))
  1426.          (day (extract-calendar-day today))
  1427.          (year (extract-calendar-year today))
  1428.          (today-visible
  1429.           (or (not mon)
  1430.               (let ((offset (calendar-interval mon yr month year)))
  1431.                 (and (<= offset 1) (>= offset -1)))))
  1432.          (day-in-week (calendar-day-of-week today)))
  1433.     (update-calendar-mode-line)
  1434.     (if mon
  1435.         (generate-calendar mon yr)
  1436.         (generate-calendar month year))
  1437.     (calendar-cursor-to-visible-date
  1438.      (if today-visible today (list displayed-month 1 displayed-year)))
  1439.     (set-buffer-modified-p nil)
  1440.     (or (one-window-p t)
  1441.         (/= (frame-width) (window-width))
  1442.         (shrink-window (- (window-height) 9)))
  1443.     (sit-for 0)
  1444.     (and mark-holidays-in-calendar
  1445.          (mark-calendar-holidays)
  1446.          (sit-for 0))
  1447.     (unwind-protect
  1448.         (if mark-diary-entries-in-calendar (mark-diary-entries))
  1449.       (if today-visible
  1450.           (run-hooks 'today-visible-calendar-hook)
  1451.         (run-hooks 'today-invisible-calendar-hook)))))
  1452.  
  1453. (defun generate-calendar (month year)
  1454.   "Generate a three-month Gregorian calendar centered around MONTH, YEAR."
  1455.   (if (< (+ month (* 12 (1- year))) 2)
  1456.       (error "Months before February, 1 AD are not available."))
  1457.   (setq displayed-month month)
  1458.   (setq displayed-year year)
  1459.   (erase-buffer)
  1460.   (increment-calendar-month month year -1)
  1461.   (calendar-for-loop i from 0 to 2 do
  1462.        (generate-calendar-month month year (+ 5 (* 25 i)))
  1463.        (increment-calendar-month month year 1)))
  1464.  
  1465. (defun generate-calendar-month (month year indent)
  1466.   "Produce a calendar for MONTH, YEAR on the Gregorian calendar.
  1467. The calendar is inserted in the buffer starting at the line on which point
  1468. is currently located, but indented INDENT spaces.  The indentation is done
  1469. from the first character on the line and does not disturb the first INDENT
  1470. characters on the line."
  1471.   (let* ((blank-days;; at start of month
  1472.           (mod
  1473.            (- (calendar-day-of-week (list month 1 year))
  1474.               calendar-week-start-day)
  1475.            7))
  1476.      (last (calendar-last-day-of-month month year)))
  1477.    (goto-char (point-min))
  1478.    (calendar-insert-indented
  1479.     (calendar-string-spread
  1480.      (list "" (format "%s %d" (calendar-month-name month) year) "") ?  20)
  1481.     indent t)
  1482.    (calendar-insert-indented "" indent);; Go to proper spot
  1483.    (calendar-for-loop i from 0 to 6 do
  1484.       (insert (substring (aref calendar-day-name-array 
  1485.                                (mod (+ calendar-week-start-day i) 7))
  1486.                          0 2))
  1487.       (insert " "))
  1488.    (calendar-insert-indented "" 0 t);; Force onto following line
  1489.    (calendar-insert-indented "" indent);; Go to proper spot
  1490.    ;; Add blank days before the first of the month
  1491.    (calendar-for-loop i from 1 to blank-days do (insert "   "))
  1492.    ;; Put in the days of the month
  1493.    (calendar-for-loop i from 1 to last do
  1494.       (insert (format "%2d " i))
  1495.       (put-text-property (- (point) (if (< i 10) 2 3)) (1- (point))
  1496.              'highlight t)
  1497.       (and (zerop (mod (+ i blank-days) 7))
  1498.            (/= i last)
  1499.            (calendar-insert-indented "" 0 t)    ;; Force onto following line
  1500.            (calendar-insert-indented "" indent)))));; Go to proper spot
  1501.  
  1502. (defun calendar-insert-indented (string indent &optional newline)
  1503.   "Insert STRING at column INDENT.
  1504. If the optional parameter NEWLINE is t, leave point at start of next line,
  1505. inserting a newline if there was no next line; otherwise, leave point after
  1506. the inserted text.  Value is always t."
  1507.   ;; Try to move to that column.
  1508.   (move-to-column indent)
  1509.   ;; If line is too short, indent out to that column.
  1510.   (if (< (current-column) indent)
  1511.       (indent-to indent))
  1512.   (insert string)
  1513.   ;; Advance to next line, if requested.
  1514.   (if newline
  1515.       (progn
  1516.     (end-of-line)
  1517.     (if (eobp)
  1518.             (newline)
  1519.       (forward-line 1))))
  1520.   t)
  1521.  
  1522. (defun redraw-calendar ()
  1523.   "Redraw the calendar display."
  1524.   (interactive)
  1525.   (let ((cursor-date (calendar-cursor-to-date)))
  1526.     (generate-calendar-window displayed-month displayed-year)
  1527.     (calendar-cursor-to-visible-date cursor-date)))
  1528.  
  1529. (defvar calendar-debug-sexp nil
  1530.   "*Turn debugging on when evaluating a sexp in the diary or holiday list.")
  1531.  
  1532. (defvar calendar-mode-map nil)
  1533. (if calendar-mode-map
  1534.     nil
  1535.   (setq calendar-mode-map (make-sparse-keymap))
  1536.   (calendar-for-loop i from 0 to 9 do
  1537.        (define-key calendar-mode-map (int-to-string i) 'digit-argument))
  1538.   (let ((l (list 'narrow-to-region 'mark-word 'mark-sexp 'mark-paragraph
  1539.                  'mark-defun 'mark-whole-buffer 'mark-page
  1540.                  'downcase-region 'upcase-region 'kill-region
  1541.                  'copy-region-as-kill 'capitalize-region 'write-region)))
  1542.     (while l
  1543.       (substitute-key-definition (car l) 'calendar-not-implemented
  1544.                  calendar-mode-map)
  1545.       (setq l (cdr l))))
  1546.   (define-key calendar-mode-map "-"     'negative-argument)
  1547.   (define-key calendar-mode-map "\C-x>" 'scroll-calendar-right)
  1548.   (define-key calendar-mode-map '[prior] 'scroll-calendar-right-three-months)
  1549.   (define-key calendar-mode-map "\M-v"   'scroll-calendar-right-three-months)
  1550.   (define-key calendar-mode-map "\C-x<" 'scroll-calendar-left)
  1551.   (define-key calendar-mode-map '[next]  'scroll-calendar-left-three-months)
  1552.   (define-key calendar-mode-map "\C-v"  'scroll-calendar-left-three-months)
  1553.   (define-key calendar-mode-map "\C-b"  'calendar-backward-day)
  1554.   (define-key calendar-mode-map "\C-p"  'calendar-backward-week)
  1555.   (define-key calendar-mode-map "\M-{"   'calendar-backward-month)
  1556.   (define-key calendar-mode-map "\C-x[" 'calendar-backward-year)
  1557.   (define-key calendar-mode-map "\C-f"  'calendar-forward-day)
  1558.   (define-key calendar-mode-map "\C-n"  'calendar-forward-week)
  1559.   (define-key calendar-mode-map '[left]  'calendar-backward-day)
  1560.   (define-key calendar-mode-map '[up]    'calendar-backward-week)
  1561.   (define-key calendar-mode-map '[right] 'calendar-forward-day)
  1562.   (define-key calendar-mode-map '[down]  'calendar-forward-week)
  1563.   (define-key calendar-mode-map "\M-}"   'calendar-forward-month)
  1564.   (define-key calendar-mode-map "\C-x]" 'calendar-forward-year)
  1565.   (define-key calendar-mode-map "\C-a"  'calendar-beginning-of-week)
  1566.   (define-key calendar-mode-map "\C-e"  'calendar-end-of-week)
  1567.   (define-key calendar-mode-map "\M-a"   'calendar-beginning-of-month)
  1568.   (define-key calendar-mode-map "\M-e"   'calendar-end-of-month)
  1569.   (define-key calendar-mode-map "\M-<"   'calendar-beginning-of-year)
  1570.   (define-key calendar-mode-map "\M->"   'calendar-end-of-year)
  1571.   (define-key calendar-mode-map "\C-@"  'calendar-set-mark)
  1572.   ;; Many people are used to typing C-SPC and getting C-@.
  1573.   (define-key calendar-mode-map "\C- " 'calendar-set-mark)
  1574.   (define-key calendar-mode-map "\C-x\C-x" 'calendar-exchange-point-and-mark)
  1575.   (define-key calendar-mode-map "\M-=" 'calendar-count-days-region)
  1576.   (define-key calendar-mode-map "gd"  'calendar-goto-date)
  1577.   (define-key calendar-mode-map "gj"  'calendar-goto-julian-date)
  1578.   (define-key calendar-mode-map "ga"  'calendar-goto-astro-day-number)
  1579.   (define-key calendar-mode-map "gh"  'calendar-goto-hebrew-date)
  1580.   (define-key calendar-mode-map "gi"  'calendar-goto-islamic-date)
  1581.   (define-key calendar-mode-map "gc"  'calendar-goto-iso-date)
  1582.   (define-key calendar-mode-map "gf"  'calendar-goto-french-date)
  1583.   (define-key calendar-mode-map "gml"  'calendar-goto-mayan-long-count-date)
  1584.   (define-key calendar-mode-map "gmpc" 'calendar-previous-calendar-round-date)
  1585.   (define-key calendar-mode-map "gmnc" 'calendar-next-calendar-round-date)
  1586.   (define-key calendar-mode-map "gmph" 'calendar-previous-haab-date)
  1587.   (define-key calendar-mode-map "gmnh" 'calendar-next-haab-date)
  1588.   (define-key calendar-mode-map "gmpt" 'calendar-previous-tzolkin-date)
  1589.   (define-key calendar-mode-map "gmnt" 'calendar-next-tzolkin-date)
  1590.   (define-key calendar-mode-map "S"   'calendar-sunrise-sunset)
  1591.   (define-key calendar-mode-map "M"   'calendar-phases-of-moon)
  1592.   (define-key calendar-mode-map " "   'scroll-other-window)
  1593.   (define-key calendar-mode-map "\C-c\C-l" 'redraw-calendar)
  1594.   (define-key calendar-mode-map "."   'calendar-goto-today)
  1595.   (define-key calendar-mode-map "o"   'calendar-other-month)
  1596.   (define-key calendar-mode-map "q"   'exit-calendar)
  1597.   (define-key calendar-mode-map "a"   'list-calendar-holidays)
  1598.   (define-key calendar-mode-map "h"   'calendar-cursor-holidays)
  1599.   (define-key calendar-mode-map "x"   'mark-calendar-holidays)
  1600.   (define-key calendar-mode-map "u"   'calendar-unmark)
  1601.   (define-key calendar-mode-map "m"   'mark-diary-entries)
  1602.   (define-key calendar-mode-map "d"   'view-diary-entries)
  1603.   (define-key calendar-mode-map "D"   'view-other-diary-entries)
  1604.   (define-key calendar-mode-map "s"   'show-all-diary-entries)
  1605.   (define-key calendar-mode-map "pd"  'calendar-print-day-of-year)
  1606.   (define-key calendar-mode-map "pc"  'calendar-print-iso-date)
  1607.   (define-key calendar-mode-map "pj"  'calendar-print-julian-date)
  1608.   (define-key calendar-mode-map "pa"  'calendar-print-astro-day-number)
  1609.   (define-key calendar-mode-map "ph"  'calendar-print-hebrew-date)
  1610.   (define-key calendar-mode-map "pi"  'calendar-print-islamic-date)
  1611.   (define-key calendar-mode-map "pf"  'calendar-print-french-date)
  1612.   (define-key calendar-mode-map "pm"  'calendar-print-mayan-date)
  1613.   (define-key calendar-mode-map "id"  'insert-diary-entry)
  1614.   (define-key calendar-mode-map "iw"  'insert-weekly-diary-entry)
  1615.   (define-key calendar-mode-map "im"  'insert-monthly-diary-entry)
  1616.   (define-key calendar-mode-map "iy"  'insert-yearly-diary-entry)
  1617.   (define-key calendar-mode-map "ia"  'insert-anniversary-diary-entry)
  1618.   (define-key calendar-mode-map "ib"  'insert-block-diary-entry)
  1619.   (define-key calendar-mode-map "ic"  'insert-cyclic-diary-entry)
  1620.   (define-key calendar-mode-map "ihd" 'insert-hebrew-diary-entry)
  1621.   (define-key calendar-mode-map "ihm" 'insert-monthly-hebrew-diary-entry)
  1622.   (define-key calendar-mode-map "ihy" 'insert-yearly-hebrew-diary-entry)
  1623.   (define-key calendar-mode-map "iid" 'insert-islamic-diary-entry)
  1624.   (define-key calendar-mode-map "iim" 'insert-monthly-islamic-diary-entry)
  1625.   (define-key calendar-mode-map "iiy" 'insert-yearly-islamic-diary-entry)
  1626.   (define-key calendar-mode-map "?"   'calendar-goto-info-node))
  1627.  
  1628. (defun describe-calendar-mode ()
  1629.   "Create a help buffer with a brief description of the calendar-mode."
  1630.   (interactive)
  1631.   (with-output-to-temp-buffer "*Help*"
  1632.     (princ
  1633.      (format
  1634.       "Calendar Mode:\nFor a complete description, type %s\n%s\n"
  1635.       (substitute-command-keys
  1636.        "\\<calendar-mode-map>\\[describe-mode] from within the calendar")
  1637.       (substitute-command-keys "\\{calendar-mode-map}")))
  1638.     (save-excursion
  1639.       (set-buffer standard-output)
  1640.       (help-mode))
  1641.     (print-help-return-message)))
  1642.  
  1643. ;; Calendar mode is suitable only for specially formatted data.
  1644. (put 'calendar-mode 'mode-class 'special)
  1645.  
  1646. (defvar calendar-mode-line-format
  1647.   (list
  1648.    (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-left]")
  1649.    "Calendar"
  1650.    (substitute-command-keys "\\<calendar-mode-map>\\[calendar-goto-info-node] info/\\[calendar-other-month] other/\\[calendar-goto-today] today")
  1651.    '(calendar-date-string (calendar-current-date) t)
  1652.    (substitute-command-keys "\\<calendar-mode-map>\\[scroll-calendar-right]"))
  1653.   "The mode line of the calendar buffer.")
  1654.  
  1655. (defun calendar-goto-info-node ()
  1656.   "Go to the info node for the calendar."
  1657.   (interactive)
  1658.   (require 'info)
  1659.   (let ((where (Info-find-emacs-command-nodes 'calendar)))
  1660.     (if (not where)
  1661.         (error "Couldn't find documentation for the calendar.")
  1662.       (save-window-excursion (info))
  1663.       (pop-to-buffer "*info*")
  1664.       (Info-find-node (car (car where)) (car (cdr (car where)))))))
  1665.  
  1666. (defun calendar-mode ()
  1667.   "A major mode for the calendar window.
  1668.  
  1669. For a complete description, type \
  1670. \\<calendar-mode-map>\\[calendar-goto-info-node] from within the calendar.
  1671.  
  1672. \\<calendar-mode-map>\\{calendar-mode-map}"
  1673.  
  1674.   (kill-all-local-variables)
  1675.   (setq major-mode 'calendar-mode)
  1676.   (setq mode-name "Calendar")
  1677.   (use-local-map calendar-mode-map)
  1678.   (setq buffer-read-only t)
  1679.   (setq indent-tabs-mode nil)
  1680.   (update-calendar-mode-line)
  1681.   (if (and (string-match "XEmacs" emacs-version)
  1682.        (featurep 'menubar) current-menubar)
  1683.       (progn
  1684.     (require 'cal-xemacs)
  1685.     (calendar-add-menus)
  1686.     (make-variable-buffer-local 'scroll-on-clipped-lines)
  1687.     (setq scroll-on-clipped-lines nil)))
  1688.   (make-local-variable 'calendar-mark-ring)
  1689.   (make-local-variable 'displayed-month);;  Month in middle of window.
  1690.   (make-local-variable 'displayed-year));;  Year in middle of window.
  1691.  
  1692. (defun calendar-string-spread (strings char length)
  1693.   "Concatenate list of STRINGS separated with copies of CHAR to fill LENGTH
  1694. There must be at least 2 strings.  The effect is like mapconcat but the
  1695. separating pieces are as balanced as possible.  Each item of STRINGS is
  1696. evaluated before concatenation so it can actually be an expression that
  1697. evaluates to a string.  If LENGTH is too short, the STRINGS are just
  1698. concatenated and the result truncated."
  1699. ;; The algorithm is based on equation (3.25) on page 85 of Concrete
  1700. ;; Mathematics by Ronald L. Graham, Donald E. Knuth, and Oren Patashnik,
  1701. ;; Addison-Wesley, Reading, MA, 1989
  1702.   (let* ((strings (mapcar 'eval strings))
  1703.          (n (- length (length (apply 'concat strings))))
  1704.          (m (1- (length strings)))
  1705.          (s (car strings))
  1706.          (strings (cdr strings))
  1707.          (i 0))
  1708.     (while strings
  1709.       (setq s (concat s
  1710.                       (make-string (max 0 (/ (+ n i) m)) char)
  1711.                       (car strings)))
  1712.       (setq i (1+ i))
  1713.       (setq strings (cdr strings)))
  1714.     (substring s 0 length)))
  1715.  
  1716. (defun update-calendar-mode-line ()
  1717.   "Update the calendar mode line with the current date and date style."
  1718.   (if (bufferp (get-buffer calendar-buffer))
  1719.       (save-excursion
  1720.         (set-buffer calendar-buffer)
  1721.         (setq mode-line-format
  1722.               (calendar-string-spread
  1723.                calendar-mode-line-format ?  (frame-width))))))
  1724.  
  1725. (defun calendar-window-list ()
  1726.   "List of all calendar-related windows."
  1727.   (let ((calendar-buffers (calendar-buffer-list))
  1728.         list)
  1729.     (walk-windows '(lambda (w)
  1730.                      (if (memq (window-buffer w) calendar-buffers)
  1731.                          (setq list (cons w list))))
  1732.                   nil t)
  1733.     list))
  1734.  
  1735. (defun calendar-buffer-list ()
  1736.   "List of all calendar-related buffers."
  1737.   (let* ((diary-buffer (get-file-buffer diary-file))
  1738.          (buffers (list "*Yahrzeits*" lunar-phases-buffer holiday-buffer
  1739.                         fancy-diary-buffer diary-buffer calendar-buffer))
  1740.          (buffer-list nil)
  1741.          b)
  1742.     (while buffers
  1743.       (setq b (car buffers))
  1744.       (setq b (cond ((stringp b) (get-buffer b))
  1745.                     ((bufferp b) b)
  1746.                     (t nil)))
  1747.       (if b (setq buffer-list (cons b buffer-list)))
  1748.       (setq buffers (cdr buffers)))
  1749.     buffer-list))
  1750.  
  1751. (defun exit-calendar ()
  1752.   "Get out of the calendar window and hide it and related buffers."
  1753.   (interactive)
  1754.   (let* ((diary-buffer (get-file-buffer diary-file)))
  1755.     (if (and diary-buffer (buffer-modified-p diary-buffer)
  1756.          (not
  1757.           (yes-or-no-p
  1758.            "Diary modified; do you really want to exit the calendar? ")))
  1759.     (error)
  1760.       ;; Need to do this multiple times because one time can replace some
  1761.       ;; calendar-related buffers with other calendar-related buffers
  1762.       (mapcar (lambda (x)
  1763.                 (mapcar 'calendar-hide-window (calendar-window-list)))
  1764.               (calendar-window-list)))))
  1765.  
  1766. (defun calendar-hide-window (window)
  1767.   "Hide WINDOW if it is calendar-related."
  1768.   (let ((buffer (if (window-live-p window) (window-buffer window))))
  1769.     (if (memq buffer (calendar-buffer-list))
  1770.         (cond
  1771.          ((and window-system
  1772.                (eq 'icon (cdr (assoc 'visibility
  1773.                                      (frame-parameters
  1774.                                       (window-frame window))))))
  1775.           nil)
  1776.          ((and window-system (window-dedicated-p window))
  1777.           (iconify-frame (window-frame window)))
  1778.          ((not (and (select-window window) (one-window-p window)))
  1779.           (delete-window window))
  1780.          (t (set-buffer buffer)
  1781.             (bury-buffer))))))
  1782.  
  1783. (defun calendar-goto-today ()
  1784.   "Reposition the calendar window so the current date is visible."
  1785.   (interactive)
  1786.   (let ((today (calendar-current-date)));; The date might have changed.
  1787.     (if (not (calendar-date-is-visible-p today))
  1788.         (generate-calendar-window)
  1789.       (update-calendar-mode-line)
  1790.       (calendar-cursor-to-visible-date today))))
  1791.  
  1792. (defun calendar-forward-month (arg)
  1793.   "Move the cursor forward ARG months.
  1794. Movement is backward if ARG is negative."
  1795.   (interactive "p")
  1796.   (calendar-cursor-to-nearest-date)
  1797.   (let* ((cursor-date (calendar-cursor-to-date t))
  1798.          (month (extract-calendar-month cursor-date))
  1799.          (day (extract-calendar-day cursor-date))
  1800.          (year (extract-calendar-year cursor-date)))
  1801.     (increment-calendar-month month year arg)
  1802.     (let ((last (calendar-last-day-of-month month year)))
  1803.       (if (< last day)
  1804.         (setq day last)))
  1805.     ;; Put the new month on the screen, if needed, and go to the new date.
  1806.     (let ((new-cursor-date (list month day year)))
  1807.       (if (not (calendar-date-is-visible-p new-cursor-date))
  1808.           (calendar-other-month month year))
  1809.       (calendar-cursor-to-visible-date new-cursor-date))))
  1810.  
  1811. (defun calendar-forward-year (arg)
  1812.   "Move the cursor forward by ARG years.
  1813. Movement is backward if ARG is negative."
  1814.   (interactive "p")
  1815.   (calendar-forward-month (* 12 arg)))
  1816.  
  1817. (defun calendar-backward-month (arg)
  1818.   "Move the cursor backward by ARG months.
  1819. Movement is forward if ARG is negative."
  1820.   (interactive "p")
  1821.   (calendar-forward-month (- arg)))
  1822.  
  1823. (defun calendar-backward-year (arg)
  1824.   "Move the cursor backward ARG years.
  1825. Movement is forward is ARG is negative."
  1826.   (interactive "p")
  1827.   (calendar-forward-month (* -12 arg)))
  1828.  
  1829. (defun scroll-calendar-left (arg)
  1830.   "Scroll the displayed calendar left by ARG months.
  1831. If ARG is negative the calendar is scrolled right.  Maintains the relative
  1832. position of the cursor with respect to the calendar as well as possible."
  1833.   (interactive "p")
  1834.   (calendar-cursor-to-nearest-date)
  1835.   (let ((old-date (calendar-cursor-to-date))
  1836.         (today (calendar-current-date)))
  1837.     (if (/= arg 0)
  1838.         (progn
  1839.           (increment-calendar-month displayed-month displayed-year arg)
  1840.           (generate-calendar-window displayed-month displayed-year)
  1841.           (calendar-cursor-to-visible-date
  1842.            (cond
  1843.             ((calendar-date-is-visible-p old-date) old-date)
  1844.             ((calendar-date-is-visible-p today) today)
  1845.             (t (list displayed-month 1 displayed-year))))))))
  1846.  
  1847. (defun scroll-calendar-right (arg)
  1848.   "Scroll the displayed calendar window right by ARG months.
  1849. If ARG is negative the calendar is scrolled left.  Maintains the relative
  1850. position of the cursor with respect to the calendar as well as possible."
  1851.   (interactive "p")
  1852.   (scroll-calendar-left (- arg)))
  1853.  
  1854. (defun scroll-calendar-left-three-months (arg)
  1855.   "Scroll the displayed calendar window left by 3*ARG months.
  1856. If ARG is negative the calendar is scrolled right.  Maintains the relative
  1857. position of the cursor with respect to the calendar as well as possible."
  1858.   (interactive "p")
  1859.   (scroll-calendar-left (* 3 arg)))
  1860.  
  1861. (defun scroll-calendar-right-three-months (arg)
  1862.   "Scroll the displayed calendar window right by 3*ARG months.
  1863. If ARG is negative the calendar is scrolled left.  Maintains the relative
  1864. position of the cursor with respect to the calendar as well as possible."
  1865.   (interactive "p")
  1866.   (scroll-calendar-left (* -3 arg)))
  1867.  
  1868. (defun calendar-current-date ()
  1869.   "Returns the current date in a list (month day year)."
  1870.   (let ((s (current-time-string)))
  1871.     (list (length (member (substring s 4 7)
  1872.                           '("Dec" "Nov" "Oct" "Sep" "Aug" "Jul"
  1873.                             "Jun" "May" "Apr" "Mar" "Feb" "Jan")))
  1874.           (string-to-number (substring s 8 10))
  1875.           (string-to-number (substring s 20 24)))))
  1876.  
  1877. (defun calendar-cursor-to-date (&optional error)
  1878.   "Returns a list (month day year) of current cursor position.
  1879. If cursor is not on a specific date, signals an error if optional parameter
  1880. ERROR is t, otherwise just returns nil."
  1881.   ;; #### This check is to avoid a race condition created by
  1882.   ;; pop-to-buffer's call to other-window interacting with the 19.13
  1883.   ;; changes allowing that to be in another frame.
  1884.   (if (not (number-or-marker-p displayed-month))
  1885.       nil
  1886.     (let* ((segment (/ (current-column) 25))
  1887.        (month (% (+ displayed-month segment -1) 12))
  1888.        (month (if (= 0 month) 12 month))
  1889.        (year
  1890.         (cond
  1891.          ((and (=  12 month) (= segment 0)) (1- displayed-year))
  1892.          ((and (=   1 month) (= segment 2)) (1+ displayed-year))
  1893.          (t displayed-year))))
  1894.       (if (and (looking-at "[0-9]")
  1895.            (< 2 (count-lines (point-min) (point))))
  1896.       (save-excursion
  1897.         (re-search-backward "[^0-9]")
  1898.         (list month
  1899.           (string-to-int (buffer-substring (1+ (point)) (+ 4 (point))))
  1900.           year))
  1901.     (if (looking-at "\\*")
  1902.         (save-excursion
  1903.           (re-search-backward "[^*]")
  1904.           (if (looking-at ".\\*\\*")
  1905.           (list month calendar-starred-day year)
  1906.         (if error (error "Not on a date!"))))
  1907.       (if error (error "Not on a date!")))))))
  1908.  
  1909. (defun calendar-cursor-to-nearest-date ()
  1910.   "Move the cursor to the closest date.
  1911. The position of the cursor is unchanged if it is already on a date.
  1912. Returns the list (month day year) giving the cursor position."
  1913.   (let ((date (calendar-cursor-to-date))
  1914.         (column (current-column)))
  1915.     (if date
  1916.         date
  1917.       (if (> 3 (count-lines (point-min) (point)))
  1918.           (progn
  1919.             (goto-line 3)
  1920.             (move-to-column column)))
  1921.       (if (not (looking-at "[0-9]"))
  1922.           (if (and (not (looking-at " *$"))
  1923.                    (or (< column 25)
  1924.                        (and (> column 27)
  1925.                             (< column 50))
  1926.                        (and (> column 52)
  1927.                             (< column 75))))
  1928.               (progn
  1929.                 (re-search-forward "[0-9]" nil t)
  1930.                 (backward-char 1))
  1931.             (re-search-backward "[0-9]" nil t)))
  1932.       (calendar-cursor-to-date))))
  1933.  
  1934. (defun calendar-forward-day (arg)
  1935.   "Move the cursor forward ARG days.
  1936. Moves backward if ARG is negative."
  1937.   (interactive "p")
  1938.   (if (/= 0 arg)
  1939.       (let*
  1940.           ((cursor-date (calendar-cursor-to-date))
  1941.            (cursor-date (if cursor-date
  1942.                             cursor-date
  1943.                           (if (> arg 0) (setq arg (1- arg)))
  1944.                           (calendar-cursor-to-nearest-date)))
  1945.            (new-cursor-date
  1946.             (calendar-gregorian-from-absolute
  1947.              (+ (calendar-absolute-from-gregorian cursor-date) arg)))
  1948.            (new-display-month (extract-calendar-month new-cursor-date))
  1949.            (new-display-year (extract-calendar-year new-cursor-date)))
  1950.         ;; Put the new month on the screen, if needed, and go to the new date.
  1951.         (if (not (calendar-date-is-visible-p new-cursor-date))
  1952.             (calendar-other-month new-display-month new-display-year))
  1953.         (calendar-cursor-to-visible-date new-cursor-date))))
  1954.  
  1955. (defun calendar-backward-day (arg)
  1956.   "Move the cursor back ARG days.
  1957. Moves forward if ARG is negative."
  1958.   (interactive "p")
  1959.   (calendar-forward-day (- arg)))
  1960.  
  1961. (defun calendar-forward-week (arg)
  1962.   "Move the cursor forward ARG weeks.
  1963. Moves backward if ARG is negative."
  1964.   (interactive "p")
  1965.   (calendar-forward-day (* arg 7)))
  1966.  
  1967. (defun calendar-backward-week (arg)
  1968.   "Move the cursor back ARG weeks.
  1969. Moves forward if ARG is negative."
  1970.   (interactive "p")
  1971.   (calendar-forward-day (* arg -7)))
  1972.  
  1973. (defun calendar-beginning-of-week (arg)
  1974.   "Move the cursor back ARG calendar-week-start-day's."
  1975.   (interactive "p")
  1976.   (calendar-cursor-to-nearest-date)
  1977.   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  1978.     (calendar-backward-day
  1979.      (if (= day calendar-week-start-day)
  1980.          (* 7 arg)
  1981.        (+ (mod (- day calendar-week-start-day) 7)
  1982.           (* 7 (1- arg)))))))
  1983.  
  1984. (defun calendar-end-of-week (arg)
  1985.   "Move the cursor forward ARG calendar-week-start-day+6's."
  1986.   (interactive "p")
  1987.   (calendar-cursor-to-nearest-date)
  1988.   (let ((day (calendar-day-of-week (calendar-cursor-to-date))))
  1989.     (calendar-forward-day
  1990.      (if (= day (mod (1- calendar-week-start-day) 7))
  1991.          (* 7 arg)
  1992.        (+ (- 6 (mod (- day calendar-week-start-day) 7))
  1993.           (* 7 (1- arg)))))))
  1994.  
  1995. (defun calendar-beginning-of-month (arg)
  1996.   "Move the cursor backward ARG month beginnings."
  1997.   (interactive "p")
  1998.   (calendar-cursor-to-nearest-date)
  1999.   (let* ((date (calendar-cursor-to-date))
  2000.          (month (extract-calendar-month date))
  2001.          (day (extract-calendar-day date))
  2002.          (year (extract-calendar-year date)))
  2003.     (if (= day 1)
  2004.         (calendar-backward-month arg)
  2005.       (calendar-cursor-to-visible-date (list month 1 year))
  2006.       (calendar-backward-month (1- arg)))))
  2007.  
  2008. (defun calendar-end-of-month (arg)
  2009.   "Move the cursor forward ARG month ends."
  2010.   (interactive "p")
  2011.   (calendar-cursor-to-nearest-date)
  2012.   (let* ((date (calendar-cursor-to-date))
  2013.          (month (extract-calendar-month date))
  2014.          (day (extract-calendar-day date))
  2015.          (year (extract-calendar-year date))
  2016.          (last-day (calendar-last-day-of-month month year)))
  2017.     (if (/= day last-day)
  2018.         (progn
  2019.           (calendar-cursor-to-visible-date (list month last-day year))
  2020.           (setq arg (1- arg))))
  2021.     (increment-calendar-month month year arg)
  2022.     (let ((last-day (list
  2023.                      month
  2024.                      (calendar-last-day-of-month month year)
  2025.                      year)))
  2026.       (if (not (calendar-date-is-visible-p last-day))
  2027.           (calendar-other-month month year)
  2028.       (calendar-cursor-to-visible-date last-day)))))
  2029.  
  2030. (defun calendar-beginning-of-year (arg)
  2031.   "Move the cursor backward ARG year beginnings."
  2032.   (interactive "p")
  2033.   (calendar-cursor-to-nearest-date)
  2034.   (let* ((date (calendar-cursor-to-date))
  2035.          (month (extract-calendar-month date))
  2036.          (day (extract-calendar-day date))
  2037.          (year (extract-calendar-year date))
  2038.          (jan-first (list 1 1 year)))
  2039.     (if (and (= day 1) (= 1 month))
  2040.         (calendar-backward-month (* 12 arg))
  2041.       (if (and (= arg 1)
  2042.                (calendar-date-is-visible-p jan-first))
  2043.           (calendar-cursor-to-visible-date jan-first)
  2044.         (calendar-other-month 1 (- year (1- arg)))))))
  2045.  
  2046. (defun calendar-end-of-year (arg)
  2047.   "Move the cursor forward ARG year beginnings."
  2048.   (interactive "p")
  2049.   (calendar-cursor-to-nearest-date)
  2050.   (let* ((date (calendar-cursor-to-date))
  2051.          (month (extract-calendar-month date))
  2052.          (day (extract-calendar-day date))
  2053.          (year (extract-calendar-year date))
  2054.          (dec-31 (list 12 31 year)))
  2055.     (if (and (= day 31) (= 12 month))
  2056.         (calendar-forward-month (* 12 arg))
  2057.       (if (and (= arg 1)
  2058.                (calendar-date-is-visible-p dec-31))
  2059.           (calendar-cursor-to-visible-date dec-31)
  2060.         (calendar-other-month 12 (- year (1- arg)))
  2061.         (calendar-cursor-to-visible-date (list 12 31 displayed-year))))))
  2062.  
  2063. ;; The following version of calendar-gregorian-from-absolute is preferred for
  2064. ;; reasons of clarity, BUT it's much slower than the version that follows it.
  2065.  
  2066. ;;(defun calendar-gregorian-from-absolute (date)
  2067. ;;  "Compute the list (month day year) corresponding to the absolute DATE.
  2068. ;;The absolute date is the number of days elapsed since the (imaginary)
  2069. ;;Gregorian date Sunday, December 31, 1 BC."
  2070. ;;  (let* ((approx (/ date 366));; Approximation from below.
  2071. ;;         (year                ;; Search forward from the approximation.
  2072. ;;          (+ approx
  2073. ;;             (calendar-sum y approx
  2074. ;;                 (>= date (calendar-absolute-from-gregorian (list 1 1 (1+ y))))
  2075. ;;                  1)))
  2076. ;;         (month         ;; Search forward from January.
  2077. ;;          (1+ (calendar-sum m 1
  2078. ;;                   (> date
  2079. ;;                      (calendar-absolute-from-gregorian
  2080. ;;                       (list m (calendar-last-day-of-month m year) year)))
  2081. ;;                   1)))
  2082. ;;         (day           ;; Calculate the day by subtraction.
  2083. ;;          (- date
  2084. ;;             (1- (calendar-absolute-from-gregorian (list month 1 year))))))
  2085. ;;    (list month day year)))
  2086.  
  2087. (defun calendar-gregorian-from-absolute (date)
  2088.   "Compute the list (month day year) corresponding to the absolute DATE.
  2089. The absolute date is the number of days elapsed since the (imaginary)
  2090. Gregorian date Sunday, December 31, 1 BC."
  2091. ;; See the footnote on page 384 of ``Calendrical Calculations, Part II:
  2092. ;; Three Historical Calendars'' by E. M. Reingold,  N. Dershowitz, and S. M.
  2093. ;; Clamen, Software--Practice and Experience, Volume 23, Number 4
  2094. ;; (April, 1993), pages 383-404 for an explanation.
  2095.   (let* ((d0 (1- date))
  2096.          (n400 (/ d0 146097))
  2097.          (d1 (% d0 146097))
  2098.          (n100 (/ d1 36524))
  2099.          (d2 (% d1 36524))
  2100.          (n4 (/ d2 1461))
  2101.          (d3 (% d2 1461))
  2102.          (n1 (/ d3 365))
  2103.          (day (1+ (% d3 365)))
  2104.          (year (+ (* 400 n400) (* 100 n100) (* n4 4) n1)))
  2105.     (if (or (= n100 4) (= n1 4))
  2106.         (list 12 31 year)
  2107.       (let ((year (1+ year))
  2108.             (month 1))
  2109.         (while (let ((mdays (calendar-last-day-of-month month year)))
  2110.                  (and (< mdays day)
  2111.                       (setq day (- day mdays))))
  2112.           (setq month (1+ month)))
  2113.         (list month day year)))))
  2114.  
  2115. (defun calendar-cursor-to-visible-date (date)
  2116.   "Move the cursor to DATE that is on the screen."
  2117.   (let* ((month (extract-calendar-month date))
  2118.      (day (extract-calendar-day date))
  2119.      (year (extract-calendar-year date))
  2120.      (first-of-month-weekday (calendar-day-of-week (list month 1 year))))
  2121.     (goto-line (+ 3
  2122.           (/ (+ day  -1
  2123.                         (mod
  2124.                          (- (calendar-day-of-week (list month 1 year))
  2125.                             calendar-week-start-day)
  2126.                          7))
  2127.                      7)))
  2128.     (move-to-column (+ 6
  2129.                (* 25
  2130.               (1+ (calendar-interval
  2131.                    displayed-month displayed-year month year)))
  2132.                (* 3 (mod
  2133.                              (- (calendar-day-of-week date)
  2134.                                 calendar-week-start-day)
  2135.                              7))))))
  2136.  
  2137. (defun calendar-other-month (month year)
  2138.   "Display a three-month calendar centered around MONTH and YEAR."
  2139.   (interactive (calendar-read-date 'noday))
  2140.   (if (and (= month displayed-month)
  2141.            (= year displayed-year))
  2142.       nil
  2143.     (let ((old-date (calendar-cursor-to-date))
  2144.           (today (calendar-current-date)))
  2145.       (generate-calendar-window month year)
  2146.       (calendar-cursor-to-visible-date
  2147.        (cond
  2148.         ((calendar-date-is-visible-p old-date) old-date)
  2149.         ((calendar-date-is-visible-p today) today)
  2150.         (t (list month 1 year)))))))
  2151.  
  2152. (defun calendar-set-mark (arg)
  2153.   "Mark the date under the cursor, or jump to marked date.
  2154. With no prefix argument, push current date onto marked date ring.
  2155. With argument, jump to mark, pop it, and put point at end of ring."
  2156.   (interactive "P")
  2157.   (let ((date (calendar-cursor-to-date t)))
  2158.     (if (null arg)
  2159.         (progn
  2160.           (setq calendar-mark-ring (cons date calendar-mark-ring))
  2161.           ;; Since the top of the mark ring is the marked date in the
  2162.           ;; calendar, the mark ring in the calendar is one longer than
  2163.           ;; in other buffers to get the same effect.
  2164.           (if (> (length calendar-mark-ring) (1+ mark-ring-max))
  2165.               (setcdr (nthcdr mark-ring-max calendar-mark-ring) nil))
  2166.           (message "Mark set"))
  2167.       (if (null calendar-mark-ring)
  2168.           (error "No mark set in this buffer")
  2169.         (calendar-goto-date (car calendar-mark-ring))
  2170.         (setq calendar-mark-ring
  2171.               (cdr (nconc calendar-mark-ring (list date))))))))
  2172.  
  2173. (defun calendar-exchange-point-and-mark ()
  2174.   "Exchange the current cursor position with the marked date."
  2175.   (interactive)
  2176.   (let ((mark (car calendar-mark-ring))
  2177.         (date (calendar-cursor-to-date t)))
  2178.     (if (null mark)
  2179.         (error "No mark set in this buffer")
  2180.       (setq calendar-mark-ring (cons date (cdr calendar-mark-ring)))
  2181.       (calendar-goto-date mark))))
  2182.  
  2183. (defun calendar-count-days-region ()
  2184.   "Count the number of days (inclusive) between point and the mark."
  2185.   (interactive)
  2186.   (let* ((days (- (calendar-absolute-from-gregorian
  2187.                    (calendar-cursor-to-date t))
  2188.                   (calendar-absolute-from-gregorian
  2189.                    (or (car calendar-mark-ring)
  2190.                        (error "No mark set in this buffer")))))
  2191.          (days (1+ (if (> days 0) days (- days)))))
  2192.     (message "Region has %d day%s (inclusive)"
  2193.              days (if (> days 1) "s" ""))))
  2194.  
  2195. (defun calendar-not-implemented ()
  2196.   "Not implemented."
  2197.   (interactive)
  2198.   (error "%s not available in the calendar"
  2199.          (global-key-binding (this-command-keys))))
  2200.  
  2201. (defun calendar-read (prompt acceptable &optional initial-contents)
  2202.   "Return an object read from the minibuffer.
  2203. Prompt with the string PROMPT and use the function ACCEPTABLE to decide if
  2204. entered item is acceptable.  If non-nil, optional third arg INITIAL-CONTENTS
  2205. is a string to insert in the minibuffer before reading."
  2206.   (let ((value (read-minibuffer prompt initial-contents)))
  2207.     (while (not (funcall acceptable value))
  2208.       (setq value (read-minibuffer prompt initial-contents)))
  2209.     value))
  2210.  
  2211. (defun calendar-read-date (&optional noday)
  2212.   "Prompt for Gregorian date.  Returns a list (month day year).
  2213. If optional NODAY is t, does not ask for day, but just returns
  2214. (month nil year); if NODAY is any other non-nil value the value returned is
  2215. (month year) "
  2216.   (let* ((year (calendar-read
  2217.                 "Year (>0): "
  2218.                 '(lambda (x) (> x 0))
  2219.                 (int-to-string (extract-calendar-year
  2220.                                 (calendar-current-date)))))
  2221.          (month-array calendar-month-name-array)
  2222.          (completion-ignore-case t)
  2223.          (month (cdr (assoc
  2224.                       (capitalize
  2225.                        (completing-read
  2226.                         "Month name: "
  2227.                         (mapcar 'list (append month-array nil))
  2228.                         nil t))
  2229.                       (calendar-make-alist month-array 1 'capitalize))))
  2230.          (last (calendar-last-day-of-month month year)))
  2231.     (if noday
  2232.         (if (eq noday t)
  2233.             (list month nil year)
  2234.           (list month year))
  2235.       (list month
  2236.             (calendar-read (format "Day (1-%d): " last)
  2237.                                    '(lambda (x) (and (< 0 x) (<= x last))))
  2238.             year))))
  2239.  
  2240. (defun calendar-goto-date (date)
  2241.   "Move cursor to DATE."
  2242.   (interactive (list (calendar-read-date)))
  2243.   (let ((month (extract-calendar-month date))
  2244.         (year (extract-calendar-year date)))
  2245.     (if (not (calendar-date-is-visible-p date))
  2246.         (calendar-other-month
  2247.          (if (and (= month 1) (= year 1))
  2248.              2
  2249.            month)
  2250.          year)))
  2251.   (calendar-cursor-to-visible-date date))
  2252.  
  2253. (defun calendar-goto-julian-date (date &optional noecho)
  2254.   "Move cursor to Julian DATE; echo Julian date unless NOECHO is t."
  2255.   (interactive
  2256.    (let* ((today (calendar-current-date))
  2257.           (year (calendar-read
  2258.                  "Julian calendar year (>0): "
  2259.                  '(lambda (x) (> x 0))
  2260.                  (int-to-string
  2261.                   (extract-calendar-year
  2262.                    (calendar-julian-from-absolute
  2263.                     (calendar-absolute-from-gregorian
  2264.                      today))))))
  2265.           (month-array calendar-month-name-array)
  2266.           (completion-ignore-case t)
  2267.           (month (cdr (assoc
  2268.                        (capitalize
  2269.                         (completing-read
  2270.                          "Julian calendar month name: "
  2271.                          (mapcar 'list (append month-array nil))
  2272.                          nil t))
  2273.                        (calendar-make-alist month-array 1 'capitalize))))
  2274.           (last 
  2275.            (if (and (zerop (% year 4)) (= month 2))
  2276.                29
  2277.              (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
  2278.           (day (calendar-read
  2279.                 (format "Julian calendar day (%d-%d): "
  2280.                         (if (and (= year 1) (= month 1)) 3 1) last)
  2281.                 '(lambda (x) 
  2282.                    (and (< (if (and (= year 1) (= month 1)) 2 0) x)
  2283.                         (<= x last))))))
  2284.      (list (list month day year))))
  2285.   (calendar-goto-date (calendar-gregorian-from-absolute
  2286.                        (calendar-absolute-from-julian date)))
  2287.   (or noecho (calendar-print-julian-date)))
  2288.  
  2289. (defun calendar-goto-hebrew-date (date &optional noecho)
  2290.   "Move cursor to Hebrew DATE; echo Hebrew date unless NOECHO is t."
  2291.   (interactive
  2292.    (let* ((today (calendar-current-date))
  2293.           (year (calendar-read
  2294.                  "Hebrew calendar year (>3760): "
  2295.                  '(lambda (x) (> x 3760))
  2296.                  (int-to-string
  2297.                   (extract-calendar-year
  2298.                    (calendar-hebrew-from-absolute
  2299.                     (calendar-absolute-from-gregorian today))))))
  2300.           (month-array (if (hebrew-calendar-leap-year-p year)
  2301.                            calendar-hebrew-month-name-array-leap-year
  2302.                          calendar-hebrew-month-name-array-common-year))
  2303.           (completion-ignore-case t)
  2304.           (month (cdr (assoc
  2305.                        (capitalize
  2306.                         (completing-read
  2307.                          "Hebrew calendar month name: "
  2308.                          (mapcar 'list (append month-array nil))
  2309.                          (if (= year 3761)
  2310.                              '(lambda (x)
  2311.                                 (let ((m (cdr
  2312.                                           (assoc
  2313.                                            (car x)
  2314.                                            (calendar-make-alist
  2315.                                             month-array)))))
  2316.                                   (< 0
  2317.                                      (calendar-absolute-from-hebrew
  2318.                                       (list m
  2319.                                             (hebrew-calendar-last-day-of-month
  2320.                                              m year)
  2321.                                             year))))))
  2322.                                  
  2323.                          t))
  2324.                        (calendar-make-alist month-array 1 'capitalize))))
  2325.           (last (hebrew-calendar-last-day-of-month month year))
  2326.           (first (if (and (= year 3761) (= month 10))
  2327.                      18 1))
  2328.           (day (calendar-read
  2329.                 (format "Hebrew calendar day (%d-%d): "
  2330.                         first last)
  2331.                 '(lambda (x) (and (<= first x) (<= x last))))))
  2332.      (list (list month day year))))
  2333.   (calendar-goto-date (calendar-gregorian-from-absolute
  2334.                        (calendar-absolute-from-hebrew date)))
  2335.   (or noecho (calendar-print-hebrew-date)))
  2336.  
  2337. (defun calendar-goto-islamic-date (date &optional noecho)
  2338.   "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
  2339.   (interactive
  2340.    (let* ((today (calendar-current-date))
  2341.           (year (calendar-read
  2342.                  "Islamic calendar year (>0): "
  2343.                  '(lambda (x) (> x 0))
  2344.                  (int-to-string
  2345.                   (extract-calendar-year
  2346.                    (calendar-islamic-from-absolute
  2347.                     (calendar-absolute-from-gregorian today))))))
  2348.           (month-array calendar-islamic-month-name-array)
  2349.           (completion-ignore-case t)
  2350.           (month (cdr (assoc
  2351.                        (capitalize
  2352.                         (completing-read
  2353.                          "Islamic calendar month name: "
  2354.                          (mapcar 'list (append month-array nil))
  2355.                          nil t))
  2356.                        (calendar-make-alist month-array 1 'capitalize))))
  2357.           (last (islamic-calendar-last-day-of-month month year))
  2358.           (day (calendar-read
  2359.                 (format "Islamic calendar day (1-%d): " last)
  2360.                 '(lambda (x) (and (< 0 x) (<= x last))))))
  2361.      (list (list month day year))))
  2362.   (calendar-goto-date (calendar-gregorian-from-absolute
  2363.                        (calendar-absolute-from-islamic date)))
  2364.   (or noecho (calendar-print-islamic-date)))
  2365.  
  2366. (defun calendar-goto-iso-date (date &optional noecho)
  2367.   "Move cursor to ISO DATE; echo ISO date unless NOECHO is t."
  2368.   (interactive
  2369.    (let* ((today (calendar-current-date))
  2370.           (year (calendar-read
  2371.                  "ISO calendar year (>0): "
  2372.                  '(lambda (x) (> x 0))
  2373.                  (int-to-string (extract-calendar-year today))))
  2374.           (no-weeks (extract-calendar-month
  2375.                      (calendar-iso-from-absolute
  2376.                       (1-
  2377.                        (calendar-dayname-on-or-before
  2378.                         1 (calendar-absolute-from-gregorian
  2379.                            (list 1 4 (1+ year))))))))
  2380.           (week (calendar-read
  2381.                  (format "ISO calendar week (1-%d): " no-weeks)
  2382.                  '(lambda (x) (and (> x 0) (<= x no-weeks)))))
  2383.           (day (calendar-read
  2384.                 "ISO day (1-7): "
  2385.                 '(lambda (x) (and (<= 1 x) (<= x 7))))))
  2386.      (list (list week day year))))
  2387.   (calendar-goto-date (calendar-gregorian-from-absolute
  2388.                        (calendar-absolute-from-iso date)))
  2389.   (or noecho (calendar-print-iso-date)))
  2390.  
  2391. (defun calendar-interval (mon1 yr1 mon2 yr2)
  2392.   "The number of months difference between MON1, YR1 and MON2, YR2."
  2393.   (+ (* 12 (- yr2 yr1))
  2394.      (- mon2 mon1)))
  2395.  
  2396. (defun calendar-day-name (date)
  2397.   "Returns a string with the name of the day of the week of DATE."
  2398.   (aref calendar-day-name-array (calendar-day-of-week date)))
  2399.  
  2400. (defvar calendar-day-name-array
  2401.   ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"])
  2402.  
  2403. (defvar calendar-month-name-array
  2404.   ["January" "February" "March"     "April"   "May"      "June"
  2405.    "July"    "August"   "September" "October" "November" "December"])
  2406.  
  2407. (defun calendar-make-alist (sequence &optional start-index filter)
  2408.   "Make an assoc list corresponding to SEQUENCE.
  2409. Start at index 1, unless optional START-INDEX is provided.
  2410. If FILTER is provided, apply it to each item in the list."
  2411.   (let ((index (if start-index (1- start-index) 0)))
  2412.     (mapcar
  2413.      '(lambda (x)
  2414.         (setq index (1+ index))
  2415.         (cons (if filter (funcall filter x) x)
  2416.               index))
  2417.      (append sequence nil))))
  2418.  
  2419. (defun calendar-month-name (month)
  2420.   "The name of MONTH."
  2421.   (aref calendar-month-name-array (1- month)))
  2422.  
  2423. (defun calendar-day-of-week (date)
  2424.   "Returns the day-of-the-week index of DATE, 0 for Sunday, 1 for Monday, etc."
  2425.   (% (calendar-absolute-from-gregorian date) 7))
  2426.  
  2427. (defun calendar-unmark ()
  2428.   "Delete all diary/holiday marks/highlighting from the calendar."
  2429.   (interactive)
  2430.   (setq mark-holidays-in-calendar nil)
  2431.   (setq mark-diary-entries-in-calendar nil)
  2432.   (redraw-calendar))
  2433.  
  2434. (defun calendar-date-is-visible-p (date)
  2435.   "Returns t if DATE is legal and is visible in the calendar window."
  2436.   (let ((gap (calendar-interval
  2437.               displayed-month displayed-year
  2438.               (extract-calendar-month date) (extract-calendar-year date))))
  2439.     (and (calendar-date-is-legal-p date) (> 2 gap) (< -2 gap))))
  2440.  
  2441. (defun calendar-date-is-legal-p (date)
  2442.   "Returns t if DATE is a legal date."
  2443.   (let ((month (extract-calendar-month date))
  2444.         (day (extract-calendar-day date))
  2445.         (year (extract-calendar-year date)))
  2446.     (and (<= 1 month) (<= month 12)
  2447.          (<= 1 day) (<= day (calendar-last-day-of-month month year))
  2448.          (<= 1 year))))
  2449.  
  2450. (defun calendar-date-equal (date1 date2)
  2451.   "Returns t if the DATE1 and DATE2 are the same."
  2452.   (and
  2453.    (= (extract-calendar-month date1) (extract-calendar-month date2))
  2454.    (= (extract-calendar-day date1) (extract-calendar-day date2))
  2455.    (= (extract-calendar-year date1) (extract-calendar-year date2))))
  2456.  
  2457. (defun mark-visible-calendar-date (date &optional mark)
  2458.   "Mark DATE in the calendar window with MARK.
  2459. MARK is either a single-character string or a face.
  2460. MARK defaults to diary-entry-marker."
  2461.   (if (calendar-date-is-legal-p date)
  2462.       (save-excursion
  2463.         (set-buffer calendar-buffer)
  2464.         (calendar-cursor-to-visible-date date)
  2465.         (let ((mark (or mark diary-entry-marker)))
  2466.           (if (stringp mark)
  2467.               (let ((buffer-read-only nil))
  2468.                 (forward-char 1)
  2469.                 (delete-char 1)
  2470.                 (insert mark)
  2471.                 (forward-char -2))
  2472.  
  2473.         (set-extent-property (make-extent (1- (point)) (1+ (point)))
  2474.                  'face mark))))))
  2475.  
  2476. (defun calendar-star-date ()
  2477.   "Replace the date under the cursor in the calendar window with asterisks.
  2478. This function can be used with the today-visible-calendar-hook run after the
  2479. calendar window has been prepared."
  2480.   (let ((buffer-read-only nil))
  2481.     (make-variable-buffer-local 'calendar-starred-day)
  2482.     (forward-char 1)
  2483.     (setq calendar-starred-day
  2484.           (string-to-int
  2485.            (buffer-substring (point) (- (point) 2))))
  2486.     (delete-char -2)
  2487.     (insert "**")
  2488.     (backward-char 1)
  2489.     (set-buffer-modified-p nil)))
  2490.  
  2491. (defun calendar-mark-today ()
  2492.   "Mark the date under the cursor in the calendar window.
  2493. The date is marked with calendar-today-marker.  This function can be used with
  2494. the today-visible-calendar-hook run after the calendar window has been
  2495. prepared."
  2496.   (mark-visible-calendar-date
  2497.    (calendar-cursor-to-date)
  2498.    calendar-today-marker))
  2499.  
  2500. (defun calendar-date-compare (date1 date2)
  2501.   "Returns t if DATE1 is before DATE2, nil otherwise.
  2502. The actual dates are in the car of DATE1 and DATE2."
  2503.   (< (calendar-absolute-from-gregorian (car date1))
  2504.      (calendar-absolute-from-gregorian (car date2))))
  2505.  
  2506. (defun calendar-date-string (date &optional abbreviate nodayname)
  2507.   "A string form of DATE, driven by the variable `calendar-date-display-form'.
  2508. An optional parameter ABBREVIATE, when t, causes the month and day names to be
  2509. abbreviated to three characters.  An optional parameter NODAYNAME, when t,
  2510. omits the name of the day of the week."
  2511.   (let* ((dayname
  2512.           (if nodayname
  2513.               nil
  2514.             (if abbreviate
  2515.                 (substring (calendar-day-name date) 0 3)
  2516.               (calendar-day-name date))))
  2517.          (month (extract-calendar-month date))
  2518.          (monthname
  2519.           (if abbreviate
  2520.               (substring
  2521.                (calendar-month-name month) 0 3)
  2522.             (calendar-month-name month)))
  2523.          (day (int-to-string (extract-calendar-day date)))
  2524.          (month (int-to-string month))
  2525.          (year (int-to-string (extract-calendar-year date))))
  2526.     (mapconcat 'eval calendar-date-display-form "")))
  2527.  
  2528. (defun calendar-dayname-on-or-before (dayname date)
  2529.   "Returns the absolute date of the DAYNAME on or before absolute DATE.
  2530. DAYNAME=0 means Sunday, DAYNAME=1 means Monday, and so on.
  2531.  
  2532. Note: Applying this function to d+6 gives us the DAYNAME on or after an
  2533. absolute day d.  Similarly, applying it to d+3 gives the DAYNAME nearest to
  2534. absolute date d, applying it to d-1 gives the DAYNAME previous to absolute
  2535. date d, and applying it to d+7 gives the DAYNAME following absolute date d."
  2536.   (- date (% (- date dayname) 7)))
  2537.  
  2538. (defun calendar-nth-named-absday (n dayname month year &optional day)
  2539.   "The absolute date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
  2540. A DAYNAME of 0 means Sunday, 1 means Monday, and so on.  If N<0,
  2541. return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
  2542. If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
  2543.  
  2544. If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
  2545.   (if (> n 0)
  2546.       (+ (* 7 (1- n))
  2547.      (calendar-dayname-on-or-before
  2548.       dayname
  2549.       (+ 6 (calendar-absolute-from-gregorian
  2550.         (list month (or day 1) year)))))
  2551.     (+ (* 7 (1+ n))
  2552.        (calendar-dayname-on-or-before
  2553.     dayname
  2554.     (calendar-absolute-from-gregorian
  2555.      (list month
  2556.            (or day (calendar-last-day-of-month month year))
  2557.            year))))))
  2558.  
  2559. (defun calendar-nth-named-day (n dayname month year &optional day)
  2560.   "The date of Nth DAYNAME in MONTH, YEAR before/after optional DAY.
  2561. A DAYNAME of 0 means Sunday, 1 means Monday, and so on.  If N<0,
  2562. return the Nth DAYNAME before MONTH DAY, YEAR (inclusive).
  2563. If N>0, return the Nth DAYNAME after MONTH DAY, YEAR (inclusive).
  2564.  
  2565. If DAY is omitted, it defaults to 1 if N>0, and MONTH's last day otherwise."
  2566.   (calendar-gregorian-from-absolute
  2567.    (calendar-nth-named-absday n dayname month year day)))
  2568.  
  2569. (defun calendar-day-of-year-string (&optional date)
  2570.   "String of day number of year of Gregorian DATE.
  2571. Defaults to today's date if DATE is not given."
  2572.   (let* ((d (or date (calendar-current-date)))
  2573.          (year (extract-calendar-year d))
  2574.          (day (calendar-day-number d))
  2575.          (days-remaining (- (calendar-day-number (list 12 31 year)) day)))
  2576.     (format "Day %d of %d; %d day%s remaining in the year"
  2577.             day year days-remaining (if (= days-remaining 1) "" "s"))))
  2578.  
  2579. (defun calendar-print-day-of-year ()
  2580.   "Show day number in year/days remaining in year for date under the cursor."
  2581.   (interactive)
  2582.   (message (calendar-day-of-year-string (calendar-cursor-to-date t))))
  2583.  
  2584. (defun calendar-absolute-from-iso (date)
  2585.   "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  2586. The `ISO year' corresponds approximately to the Gregorian year, but
  2587. weeks start on Monday and end on Sunday.  The first week of the ISO year is
  2588. the first such week in which at least 4 days are in a year.  The ISO
  2589. commercial DATE has the form (week day year) in which week is in the range
  2590. 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 =
  2591. Sunday).  The Gregorian date Sunday, December 31, 1 BC is imaginary."
  2592.   (let* ((week (extract-calendar-month date))
  2593.          (day (extract-calendar-day date))
  2594.          (year (extract-calendar-year date)))
  2595.     (+ (calendar-dayname-on-or-before
  2596.         1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year))))
  2597.        (* 7 (1- week))
  2598.        (if (= day 0) 6 (1- day)))))
  2599.  
  2600. (defun calendar-iso-from-absolute (date)
  2601.   "Compute the `ISO commercial date' corresponding to the absolute DATE.
  2602. The ISO year corresponds approximately to the Gregorian year, but weeks
  2603. start on Monday and end on Sunday.  The first week of the ISO year is the
  2604. first such week in which at least 4 days are in a year.  The ISO commercial
  2605. date has the form (week day year) in which week is in the range 1..52 and
  2606. day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday).  The
  2607. absolute date is the number of days elapsed since the (imaginary) Gregorian
  2608. date Sunday, December 31, 1 BC."
  2609.   (let* ((approx (extract-calendar-year
  2610.                   (calendar-gregorian-from-absolute (- date 3))))
  2611.          (year (+ approx
  2612.                   (calendar-sum y approx
  2613.                       (>= date (calendar-absolute-from-iso (list 1 1 (1+ y))))
  2614.                       1))))
  2615.     (list
  2616.      (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7))
  2617.      (% date 7)
  2618.      year)))
  2619.  
  2620. (defun calendar-iso-date-string (&optional date)
  2621.   "String of ISO date of Gregorian DATE.
  2622. Defaults to today's date if DATE is not given."
  2623.   (let* ((d (calendar-absolute-from-gregorian 
  2624.              (or date (calendar-current-date))))
  2625.          (day (% d 7))
  2626.          (iso-date (calendar-iso-from-absolute d)))
  2627.     (format "Day %s of week %d of %d"
  2628.             (if (zerop day) 7 day)
  2629.             (extract-calendar-month iso-date)
  2630.             (extract-calendar-year iso-date))))
  2631.  
  2632. (defun calendar-print-iso-date ()
  2633.   "Show equivalent ISO date for the date under the cursor."
  2634.   (interactive)
  2635.   (message "ISO date: %s"
  2636.            (calendar-iso-date-string (calendar-cursor-to-date t))))
  2637.  
  2638. (defun calendar-julian-from-absolute (date)
  2639.   "Compute the Julian (month day year) corresponding to the absolute DATE.
  2640. The absolute date is the number of days elapsed since the (imaginary)
  2641. Gregorian date Sunday, December 31, 1 BC."
  2642.   (let* ((approx (/ (+ date 2) 366));; Approximation from below.
  2643.          (year        ;; Search forward from the approximation.
  2644.           (+ approx
  2645.              (calendar-sum y approx
  2646.                 (>= date (calendar-absolute-from-julian (list 1 1 (1+ y))))
  2647.                 1)))
  2648.          (month       ;; Search forward from January.
  2649.           (1+ (calendar-sum m 1
  2650.                  (> date
  2651.                     (calendar-absolute-from-julian
  2652.                      (list m
  2653.                            (if (and (= m 2) (= (% year 4) 0))
  2654.                                29
  2655.                              (aref [31 28 31 30 31 30 31 31 30 31 30 31]
  2656.                                    (1- m)))
  2657.                            year)))
  2658.                  1)))
  2659.          (day         ;; Calculate the day by subtraction.
  2660.           (- date (1- (calendar-absolute-from-julian (list month 1 year))))))
  2661.     (list month day year)))
  2662.  
  2663. (defun calendar-absolute-from-julian (date)
  2664.   "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
  2665. The Gregorian date Sunday, December 31, 1 BC is imaginary."
  2666.   (let ((month (extract-calendar-month date))
  2667.         (day (extract-calendar-day date))
  2668.         (year (extract-calendar-year date)))
  2669.     (+ (calendar-day-number date)
  2670.        (if (and (= (% year 100) 0)
  2671.                 (/= (% year 400) 0)
  2672.                 (> month 2))
  2673.            1 0);; Correct for Julian but not Gregorian leap year.
  2674.        (* 365 (1- year))
  2675.        (/ (1- year) 4)
  2676.        -2)))
  2677.  
  2678. (defun calendar-julian-date-string (&optional date)
  2679.   "String of Julian date of Gregorian DATE.
  2680. Defaults to today's date if DATE is not given.
  2681. Driven by the variable `calendar-date-display-form'."
  2682.   (calendar-date-string
  2683.    (calendar-julian-from-absolute
  2684.     (calendar-absolute-from-gregorian
  2685.      (or date (calendar-current-date))))
  2686.    nil t))
  2687.  
  2688. (defun calendar-print-julian-date ()
  2689.   "Show the Julian calendar equivalent of the date under the cursor."
  2690.   (interactive)
  2691.   (message "Julian date: %s"
  2692.            (calendar-julian-date-string (calendar-cursor-to-date t))))
  2693.  
  2694. (defun islamic-calendar-leap-year-p (year)
  2695.   "Returns t if YEAR is a leap year on the Islamic calendar."
  2696.   (memq (% year 30)
  2697.         (list 2 5 7 10 13 16 18 21 24 26 29)))
  2698.  
  2699. (defun islamic-calendar-last-day-of-month (month year)
  2700.   "The last day in MONTH during YEAR on the Islamic calendar."
  2701.   (cond
  2702.    ((memq month (list 1 3 5 7 9 11)) 30)
  2703.    ((memq month (list 2 4 6 8 10)) 29)
  2704.    (t (if (islamic-calendar-leap-year-p year) 30 29))))
  2705.  
  2706. (defun islamic-calendar-day-number (date)
  2707.   "Return the day number within the year of the Islamic date DATE."
  2708.     (let* ((month (extract-calendar-month date))
  2709.            (day (extract-calendar-day date)))
  2710.       (+ (* 30 (/ month 2))
  2711.          (* 29 (/ (1- month) 2))
  2712.          day)))
  2713.  
  2714. (defun calendar-absolute-from-islamic (date)
  2715.   "Absolute date of Islamic DATE.
  2716. The absolute date is the number of days elapsed since the (imaginary)
  2717. Gregorian date Sunday, December 31, 1 BC."
  2718.   (let* ((month (extract-calendar-month date))
  2719.          (day (extract-calendar-day date))
  2720.          (year (extract-calendar-year date))
  2721.          (y (% year 30))
  2722.          (leap-years-in-cycle
  2723.           (cond
  2724.            ((< y 3) 0)  ((< y 6) 1)  ((< y 8) 2)  ((< y 11) 3) ((< y 14) 4)
  2725.            ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
  2726.            (t 10))))
  2727.     (+ (islamic-calendar-day-number date);; days so far this year
  2728.        (* (1- year) 354)                 ;; days in all non-leap years
  2729.        (* 11 (/ year 30))                ;; leap days in complete cycles
  2730.        leap-years-in-cycle               ;; leap days this cycle
  2731.        227014)))                         ;; days before start of calendar
  2732.  
  2733. (defun calendar-islamic-from-absolute (date)
  2734.   "Compute the Islamic date (month day year) corresponding to absolute DATE.
  2735. The absolute date is the number of days elapsed since the (imaginary)
  2736. Gregorian date Sunday, December 31, 1 BC."
  2737.   (if (< date 227015)
  2738.       (list 0 0 0);; pre-Islamic date
  2739.     (let* ((approx (/ (- date 227014) 355));; Approximation from below.
  2740.            (year           ;; Search forward from the approximation.
  2741.             (+ approx
  2742.                (calendar-sum y approx
  2743.                              (>= date (calendar-absolute-from-islamic
  2744.                                        (list 1 1 (1+ y))))
  2745.                              1)))
  2746.            (month          ;; Search forward from Muharram.
  2747.             (1+ (calendar-sum m 1
  2748.                               (> date
  2749.                                  (calendar-absolute-from-islamic
  2750.                                   (list m
  2751.                                         (islamic-calendar-last-day-of-month
  2752.                                          m year)
  2753.                                         year)))
  2754.                               1)))
  2755.            (day            ;; Calculate the day by subtraction.
  2756.             (- date
  2757.                (1- (calendar-absolute-from-islamic (list month 1 year))))))
  2758.       (list month day year))))
  2759.  
  2760. (defvar calendar-islamic-month-name-array
  2761.   ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
  2762.    "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"])
  2763.  
  2764. (defun calendar-islamic-date-string (&optional date)
  2765.   "String of Islamic date before sunset of Gregorian DATE.
  2766. Returns the empty string if DATE is pre-Islamic.
  2767. Defaults to today's date if DATE is not given.
  2768. Driven by the variable `calendar-date-display-form'."
  2769.   (let ((calendar-month-name-array calendar-islamic-month-name-array)
  2770.         (islamic-date (calendar-islamic-from-absolute
  2771.                        (calendar-absolute-from-gregorian
  2772.                         (or date (calendar-current-date))))))
  2773.     (if (< (extract-calendar-year islamic-date) 1)
  2774.         ""
  2775.       (calendar-date-string islamic-date nil t))))
  2776.  
  2777. (defun calendar-print-islamic-date ()
  2778.   "Show the Islamic calendar equivalent of the date under the cursor."
  2779.   (interactive)
  2780.   (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
  2781.     (if (string-equal i "")
  2782.         (message "Date is pre-Islamic")
  2783.       (message "Islamic date (until sunset): %s" i))))
  2784.  
  2785. (defun calendar-hebrew-from-absolute (date)
  2786.   "Compute the Hebrew date (month day year) corresponding to absolute DATE.
  2787. The absolute date is the number of days elapsed since the (imaginary)
  2788. Gregorian date Sunday, December 31, 1 BC."
  2789.   (let* ((greg-date (calendar-gregorian-from-absolute date))
  2790.          (month (aref [9 10 11 12 1 2 3 4 7 7 7 8]
  2791.                  (1- (extract-calendar-month greg-date))))
  2792.          (day)
  2793.          (year (+ 3760 (extract-calendar-year greg-date))))
  2794.     (while (>= date (calendar-absolute-from-hebrew (list 7 1 (1+ year))))
  2795.         (setq year (1+ year)))
  2796.     (let ((length (hebrew-calendar-last-month-of-year year)))
  2797.       (while (> date
  2798.                 (calendar-absolute-from-hebrew
  2799.                  (list month
  2800.                        (hebrew-calendar-last-day-of-month month year)
  2801.                        year)))
  2802.         (setq month (1+ (% month length)))))
  2803.     (setq day (1+
  2804.                (- date (calendar-absolute-from-hebrew (list month 1 year)))))
  2805.     (list month day year)))
  2806.  
  2807. (defun hebrew-calendar-leap-year-p (year)
  2808.   "t if YEAR is a Hebrew calendar leap year."
  2809.   (< (% (1+ (* 7 year)) 19) 7))
  2810.  
  2811. (defun hebrew-calendar-last-month-of-year (year)
  2812.   "The last month of the Hebrew calendar YEAR."
  2813.   (if (hebrew-calendar-leap-year-p year)
  2814.       13
  2815.     12))
  2816.  
  2817. (defun hebrew-calendar-last-day-of-month (month year)
  2818.   "The last day of MONTH in YEAR."
  2819.   (if (or (memq month (list 2 4 6 10 13))
  2820.           (and (= month 12) (not (hebrew-calendar-leap-year-p year)))
  2821.           (and (= month 8) (not (hebrew-calendar-long-heshvan-p year)))
  2822.           (and (= month 9) (hebrew-calendar-short-kislev-p year)))
  2823.       29
  2824.     30))
  2825.  
  2826. (defun hebrew-calendar-elapsed-days (year)
  2827.   "Days from Sun. prior to start of Hebrew calendar to mean conjunction of Tishri of Hebrew YEAR."
  2828.   (let* ((months-elapsed
  2829.           (+ (* 235 (/ (1- year) 19));; Months in complete cycles so far.
  2830.              (* 12 (% (1- year) 19))      ;; Regular months in this cycle
  2831.              (/ (1+ (* 7 (% (1- year) 19))) 19)));; Leap months this cycle
  2832.          (parts-elapsed (+ 204 (* 793 (% months-elapsed 1080))))
  2833.          (hours-elapsed (+ 5
  2834.                            (* 12 months-elapsed)
  2835.                            (* 793 (/ months-elapsed 1080))
  2836.                            (/ parts-elapsed 1080)))
  2837.          (parts                                  ;; Conjunction parts
  2838.           (+ (* 1080 (% hours-elapsed 24)) (% parts-elapsed 1080)))
  2839.          (day                                    ;; Conjunction day
  2840.           (+ 1 (* 29 months-elapsed) (/ hours-elapsed 24)))
  2841.          (alternative-day
  2842.           (if (or (>= parts 19440)    ;; If the new moon is at or after midday,
  2843.                   (and (= (% day 7) 2);; ...or is on a Tuesday...
  2844.                        (>= parts 9924)  ;;    at 9 hours, 204 parts or later...
  2845.                        (not (hebrew-calendar-leap-year-p year)));; of a
  2846.                                                                 ;; common year,
  2847.                   (and (= (% day 7) 1);; ...or is on a Monday...
  2848.                        (>= parts 16789) ;;   at 15 hours, 589 parts or later...
  2849.                        (hebrew-calendar-leap-year-p (1- year))));; at the end
  2850.                                                      ;; of a leap year
  2851.        ;; Then postpone Rosh HaShanah one day
  2852.               (1+ day)
  2853.        ;; Else
  2854.             day)))
  2855.     (if ;; If Rosh HaShanah would occur on Sunday, Wednesday, or Friday
  2856.         (memq (% alternative-day 7) (list 0 3 5))
  2857.   ;; Then postpone it one (more) day and return        
  2858.         (1+ alternative-day)
  2859.   ;; Else return        
  2860.       alternative-day)))
  2861.  
  2862. (defun hebrew-calendar-days-in-year (year)
  2863.   "Number of days in Hebrew YEAR."
  2864.   (- (hebrew-calendar-elapsed-days (1+ year))
  2865.      (hebrew-calendar-elapsed-days year)))
  2866.  
  2867. (defun hebrew-calendar-long-heshvan-p (year)
  2868.   "t if Heshvan is long in Hebrew YEAR."
  2869.   (= (% (hebrew-calendar-days-in-year year) 10) 5))
  2870.  
  2871. (defun hebrew-calendar-short-kislev-p (year)
  2872.   "t if Kislev is short in Hebrew YEAR."
  2873.   (= (% (hebrew-calendar-days-in-year year) 10) 3))
  2874.  
  2875. (defun calendar-absolute-from-hebrew (date)
  2876.   "Absolute date of Hebrew DATE.
  2877. The absolute date is the number of days elapsed since the (imaginary)
  2878. Gregorian date Sunday, December 31, 1 BC."
  2879.   (let* ((month (extract-calendar-month date))
  2880.          (day (extract-calendar-day date))
  2881.          (year (extract-calendar-year date)))
  2882.     (+ day                            ;; Days so far this month.
  2883.        (if (< month 7);; before Tishri
  2884.      ;; Then add days in prior months this year before and after Nisan
  2885.            (+ (calendar-sum
  2886.                m 7 (<= m (hebrew-calendar-last-month-of-year year))
  2887.                (hebrew-calendar-last-day-of-month m year))
  2888.               (calendar-sum
  2889.                m 1 (< m month)
  2890.                (hebrew-calendar-last-day-of-month m year)))
  2891.      ;; Else add days in prior months this year
  2892.          (calendar-sum
  2893.           m 7 (< m month)
  2894.           (hebrew-calendar-last-day-of-month m year)))
  2895.     (hebrew-calendar-elapsed-days year);; Days in prior years.
  2896.     -1373429)))                        ;; Days elapsed before absolute date 1.
  2897.  
  2898. (defvar calendar-hebrew-month-name-array-common-year
  2899.   ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
  2900.    "Heshvan" "Kislev" "Teveth" "Shevat" "Adar"])
  2901.  
  2902. (defvar calendar-hebrew-month-name-array-leap-year
  2903.   ["Nisan" "Iyar" "Sivan" "Tammuz" "Av" "Elul" "Tishri"
  2904.    "Heshvan" "Kislev" "Teveth" "Shevat" "Adar I" "Adar II"])
  2905.  
  2906. (defun calendar-hebrew-date-string (&optional date)
  2907.   "String of Hebrew date before sunset of Gregorian DATE.
  2908. Defaults to today's date if DATE is not given.
  2909. Driven by the variable `calendar-date-display-form'."
  2910.   (let* ((hebrew-date (calendar-hebrew-from-absolute
  2911.                        (calendar-absolute-from-gregorian
  2912.                         (or date (calendar-current-date)))))
  2913.          (calendar-month-name-array
  2914.           (if (hebrew-calendar-leap-year-p (extract-calendar-year hebrew-date))
  2915.               calendar-hebrew-month-name-array-leap-year
  2916.             calendar-hebrew-month-name-array-common-year)))
  2917.     (calendar-date-string hebrew-date nil t)))
  2918.  
  2919. (defun calendar-print-hebrew-date ()
  2920.   "Show the Hebrew calendar equivalent of the date under the cursor."
  2921.   (interactive)
  2922.   (message "Hebrew date (until sunset): %s"
  2923.            (calendar-hebrew-date-string (calendar-cursor-to-date t))))
  2924.  
  2925. (defun hebrew-calendar-yahrzeit (death-date year)
  2926.   "Absolute date of the anniversary of Hebrew DEATH-DATE in Hebrew YEAR."
  2927.   (let* ((death-day (extract-calendar-day death-date))
  2928.          (death-month (extract-calendar-month death-date))
  2929.          (death-year (extract-calendar-year death-date)))
  2930.     (cond
  2931.      ;; If it's Heshvan 30 it depends on the first anniversary; if
  2932.      ;; that was not Heshvan 30, use the day before Kislev 1.
  2933.      ((and (= death-month 8)
  2934.            (= death-day 30)
  2935.            (not (hebrew-calendar-long-heshvan-p (1+ death-year))))
  2936.       (1- (calendar-absolute-from-hebrew (list 9 1 year))))
  2937.      ;; If it's Kislev 30 it depends on the first anniversary; if
  2938.      ;; that was not Kislev 30, use the day before Teveth 1.
  2939.      ((and (= death-month 9)
  2940.            (= death-day 30)
  2941.            (hebrew-calendar-short-kislev-p (1+ death-year)))
  2942.       (1- (calendar-absolute-from-hebrew (list 10 1 year))))
  2943.      ;; If it's Adar II, use the same day in last month of
  2944.      ;; year (Adar or Adar II).
  2945.      ((= death-month 13)
  2946.       (calendar-absolute-from-hebrew
  2947.        (list (hebrew-calendar-last-month-of-year year) death-day year)))
  2948.      ;; If it's the 30th in Adar I and year is not a leap year
  2949.      ;; (so Adar has only 29 days), use the last day in Shevat.
  2950.      ((and (= death-day 30)
  2951.            (= death-month 12)
  2952.            (not (hebrew-calendar-leap-year-p year)))
  2953.       (calendar-absolute-from-hebrew (list 11 30 year)))
  2954.      ;; In all other cases, use the normal anniversary of the date of death.
  2955.      (t (calendar-absolute-from-hebrew
  2956.          (list death-month death-day year))))))
  2957.  
  2958. (defun calendar-set-mode-line (str)
  2959.   "Set mode line to STR, centered, surrounded by dashes."
  2960.   (setq mode-line-format
  2961.         (calendar-string-spread (list "" str "") ?- (frame-width))))
  2962.  
  2963. ;;;###autoload
  2964. (defun list-yahrzeit-dates (death-date start-year end-year)
  2965.   "List Yahrzeit dates for *Gregorian* DEATH-DATE from START-YEAR to END-YEAR.
  2966. When called interactively from the calendar window, the date of death is taken
  2967. from the cursor position."
  2968.   (interactive
  2969.    (let* ((death-date
  2970.            (if (equal (current-buffer) (get-buffer calendar-buffer))
  2971.                (calendar-cursor-to-date)
  2972.              (let* ((today (calendar-current-date))
  2973.                     (year (calendar-read
  2974.                            "Year of death (>0): "
  2975.                            '(lambda (x) (> x 0))
  2976.                            (int-to-string (extract-calendar-year today))))
  2977.                     (month-array calendar-month-name-array)
  2978.                     (completion-ignore-case t)
  2979.                     (month (cdr (assoc
  2980.                                  (capitalize
  2981.                                   (completing-read
  2982.                                    "Month of death (name): "
  2983.                                    (mapcar 'list (append month-array nil))
  2984.                                    nil t))
  2985.                                  (calendar-make-alist
  2986.                                   month-array 1 'capitalize))))
  2987.                     (last (calendar-last-day-of-month month year))
  2988.                     (day (calendar-read
  2989.                           (format "Day of death (1-%d): " last)
  2990.                           '(lambda (x) (and (< 0 x) (<= x last))))))
  2991.                (list month day year))))
  2992.           (death-year (extract-calendar-year death-date))
  2993.           (start-year (calendar-read
  2994.                        (format "Starting year of Yahrzeit table (>%d): "
  2995.                                death-year)
  2996.                        '(lambda (x) (> x death-year))
  2997.                        (int-to-string (1+ death-year))))
  2998.           (end-year (calendar-read
  2999.                      (format "Ending year of Yahrzeit table (>=%d): "
  3000.                              start-year)
  3001.                        '(lambda (x) (>= x start-year)))))
  3002.    (list death-date start-year end-year)))
  3003.   (message "Computing yahrzeits...")
  3004.   (let* ((yahrzeit-buffer "*Yahrzeits*")
  3005.          (h-date (calendar-hebrew-from-absolute
  3006.                   (calendar-absolute-from-gregorian death-date)))
  3007.          (h-month (extract-calendar-month h-date))
  3008.          (h-day (extract-calendar-day h-date))
  3009.          (h-year (extract-calendar-year h-date)))
  3010.     (set-buffer (get-buffer-create yahrzeit-buffer))
  3011.     (setq buffer-read-only nil)
  3012.     (calendar-set-mode-line
  3013.      (format "Yahrzeit dates for %s = %s"
  3014.              (calendar-date-string death-date)
  3015.              (let ((calendar-month-name-array
  3016.                     (if (hebrew-calendar-leap-year-p h-year)
  3017.                         calendar-hebrew-month-name-array-leap-year
  3018.                       calendar-hebrew-month-name-array-common-year)))
  3019.                (calendar-date-string h-date nil t))))
  3020.     (erase-buffer)
  3021.     (goto-char (point-min))
  3022.     (calendar-for-loop i from start-year to end-year do
  3023.         (insert
  3024.          (calendar-date-string
  3025.           (calendar-gregorian-from-absolute
  3026.            (hebrew-calendar-yahrzeit
  3027.             h-date
  3028.             (extract-calendar-year
  3029.              (calendar-hebrew-from-absolute
  3030.               (calendar-absolute-from-gregorian (list 1 1 i))))))) "\n"))
  3031.     (goto-char (point-min))
  3032.     (set-buffer-modified-p nil)
  3033.     (setq buffer-read-only t)
  3034.     (display-buffer yahrzeit-buffer)
  3035.     (message "Computing yahrzeits...done")))
  3036.  
  3037. (defun calendar-astro-date-string (&optional date)
  3038.   "String of astronomical (Julian) day number of afternoon of Gregorian DATE.
  3039. Defaults to today's date if DATE is not given."
  3040.   (int-to-string
  3041.    (+ 1721425 (calendar-absolute-from-gregorian
  3042.                (or date (calendar-current-date))))))
  3043.  
  3044. (defun calendar-print-astro-day-number ()
  3045.   "Show astronomical (Julian) day number of afternoon on date shown by cursor."
  3046.   (interactive)
  3047.   (message
  3048.    "Astronomical (Julian) day number after noon UTC: %s"
  3049.    (calendar-astro-date-string (calendar-cursor-to-date t))))
  3050.  
  3051. (defun calendar-goto-astro-day-number (daynumber &optional noecho)
  3052.   "Move cursor to astronomical (Julian) DAYNUMBER.
  3053. Echo astronomical (Julian) day number unless NOECHO is t."
  3054.   (interactive (list (calendar-read
  3055.                       "Astronomical (Julian) day number (>1721425): "
  3056.                       '(lambda (x) (> x 1721425)))))
  3057.   (calendar-goto-date (calendar-gregorian-from-absolute (- daynumber 1721425)))
  3058.   (or noecho (calendar-print-astro-day-number)))
  3059.  
  3060. (run-hooks 'calendar-load-hook)
  3061.  
  3062. (provide 'calendar)
  3063.  
  3064. ;;; calendar.el ends here
  3065.